I want to display posts in a slider that are marked as "featured".
For this i created a post meta box with a checkbox.
Now i have the following query

SELECT wp_posts.* FROM wp_posts, wp_postmeta WHERE wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = '' AND wp_postmeta.meta_key = 'theme_featured_post' AND wp_postmeta.meta_value = '1' AND wp_posts.post_status = 'publish' AND wp_posts.post_type = 'post' AND wp_posts.post_date < NOW() ORDER BY wp_posts.post_date DESC  

But i need another value from the post_meta table as well. So how can I select the second meta_key in the query. I could query it with

get_post_meta( $postid, 'custom_image')

Cause transactions are expensive, i would like to include it in one query.
The custom_image is an image id and has its own row in the table.

  • You simply need to use the meta_query arg in your query.
    – The J
    Commented Sep 5, 2016 at 18:07

1 Answer 1


It's not a normal practice to use raw SQL in WP, unless you absolutely have to. You should be using normal WP APIs for this and optimizing after you encounter a performance issue.

Among other things APIs will transparently cache data (such as post meta) within page load and between page loads with Object Cache installed. In typical cases Object Cache will be faster than database fetch (which is the point of it).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.