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).
Related Posts:
- order by numeric value for meta value
- Return only Count from a wp_query request?
- What is the most efficient way of querying posts based on visits and date for current day?
- Query Custom Meta Value with Increment
- How to make an activities stream mixing posts and comments?
- How To Get Some Data From WordPress Database Using WordPress $wpdb Query?
- Select from wp_post and multiple meta_value from wp_postmeta
- Pull post meta with post_query?
- How to correctly pass values to wpdb->prepare()?
- Custom Query num_rows returns wrong amount
- Iterating through $wpdb query without using get_results for large query results
- Which is faster wpdb & get_row or wp_query & ge_post_meta?
- WP_Query meta compare must include ALL array values
- Disable (or limit) queries when certain content (or data) is not needed (or showed)
- Any way to use FETCH_KEY_PAIR with $wpdb?
- Doing $wpdb->get_results returns NULL, doing the same query in my DB returns correct value
- Query against multiple locations within single custom post type post
- I need query_posts() to order results first by a meta value and then by post ID
- WP_Query with child element
- wpdb get_results() returns only 2 rows
- How to get several fields from wp_query?
- How to query post ids liked by the Author
- query by meta value then date and not empty meta value
- Query posts by meta value and sort by another meta key
- How much does $wpdb->prepare(), then $wpdb->query() VS straight $wpdb->query(), can slow down the load time of whole page
- how to use transient method?
- How to get posts that have certain meta key value and order based on another meta key’s value
- When should you use WP_Query vs query_posts() vs get_posts()?
- How to only display posts whose meta_value field is not empty?
- How to print the excuted sql right after its execution
- Can wp_query return posts meta in a single request?
- Wp get all the sub pages of the parent using wp query
- How do I query for posts by partial meta key?
- Pagination with custom SQL query
- Use REGEXP in WP_Query meta_query key
- How to query for most viewed posts and show top 5
- WordPress retrieving meta data for all custom post types in list view
- WP_Query vs get_posts
- Order by optional meta key?
- meta_query: using BETWEEN with floats and/or casting to DECIMAL
- WP_Comment_Query pagination, delving into the unknown
- Usage of the new “posts_clauses” filter in WordPress 3.1?
- Reduce or prevent calling of update_meta_cache
- Make a WP Query search match exactly the search term
- WP_Query: query posts by ids from array?
- Is there a way to extend WP_query so Custom Post Types can have properties?
- Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
- Sort posts by category name and title
- how does $wpdb differ to WP_Query?
- How to uniquely identify queries?
- Custom WP_Query order by post_meta and (author) user_meta
- Query WooCommerce orders where meta data does not exist
- Getting attachments by meta value
- WP_Query for WooCommerce Products
- pre_get_posts with get_posts
- WP_Query displaying ALL posts
- Is it true $wpdb->get_results is faster than WP_Query in most cases?
- Query causing load because of SQL_CALC_FOUND_ROWS post counting?
- meta_query where value is equal to given value
- author.php with ACF and CPTs
- Export wordpress table to excel
- WP-CLI How to generate a list of posts with corresponding meta values
- When should you use wp_reset_postdata vs wp_reset_query?
- Get posts by meta data OR title
- Use WP_Query with a custom SQL query
- Identify which loop you are hooking into; primary or secondary?
- Perform query with meta_value date
- WP_Query ordered by custom field that is a date string?
- Duplicate Queries
- WPDB Multiple site’s posts and get featured images
- How to query for a week using key => value WP_Query argument notation?
- Finding all results from database within 500 miles of the given latitude and longitude [closed]
- What is an efficient way to query based on post_meta?
- Show only oldest post by author
- Order posts by tags count?
- How can I connect to another WP database and use WP_Query?
- WP_Query not working as expected for attachments and custom meta_query
- SQL query equivalent to WP User Query
- Add and in the header while looping over custom query in page template
- How to get the posts published in last two days using WP_Query?
- How to count post meta key values for all posts in database
- Highlighting Sub topic in a post?
- Which custom query am I in and how can I access its properties & methods?
- How to count post type that has a particular term?
- Group posts by meta_key
- How should I use posts_where to change meta_value from a string to integer?
- Finding the next 5 posts
- How can I create a WP_Query that returns posts where one meta_value
- how to retrieve specific product attribute value in an sql query?
- WordPress altering my custom query, How to fix it?
- WP Meta Query for some meta (array) values
- Get number of comments after modifying comment query
- How to change a custom query into a standard loop?
- WP Query post meta value
- Dynamically Override Fancy Title – Part II
- Would this post meta be better added to the post table rather than post_meta table
- Custom URl parameter
- Transient pagination not working properly
- query posts in functions.php and update a field
- Foreach-generated custom tax queries, each with an ajax “Load more” button