Using $wpdb to fetch posts with meta data

I have a post type "shows" with 500 posts in it. I want to list all 400 in one page listing their title + 5 meta fields and a url to the show post thumbnail.

I initially went the classic wordpress way by using a custom WP_Query, looping through them with appropriate template tags such as the_title() and get_post_meta(). But the page loads very slowly. So I've done some profiling with xdebug+webgrind and it turns out that it is expensive to run these template tags when multiplied by 500 (go figure). It actually takes 3500ms just to run that query, with the thumbnail being the most expensive one.

So what I'm thinking is that if I can fetch alle the meta as part of my database query, then i would not have to run all the functions, I'd just read from the database result directly. But for that to work i need to get a return from $wpdb that includes the specific posts + the specific meta sets. Now I'm kinda of a noob when it comes to sql.

2 questions:

  1. Am I right to think that I can optimise the performance with $wpdb instead of WP_Query?
  2. What would the $wpdb method look like in order to join results from the post table and the postmeta table?

Thank you

Topic join-tables wpdb posts Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.