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
- WP_Comment_Query pagination, delving into the unknown
- Sort posts by category name and title
- WP_Query displaying ALL posts
- Query causing load because of SQL_CALC_FOUND_ROWS post counting?
- WPDB Multiple site’s posts and get featured images
- What is an efficient way to query based on post_meta?
- Highlighting Sub topic in a post?
- how to retrieve specific product attribute value in an sql query?
- Best approach to create Hot and Trending sections
- WordPress custom archive page
- Retrieve or Query Pages by ID
- Order posts by meta value and Date
- How to make orderby ‘meta_value_num’ OPTIONAL?
- How to implement time filter to show random post 1 month for one category and 3 months for other categories
- Get the Plugin Which Triggered a MySql Query in WordPress?
- How to do a query on custom taxonomies that is uncategorised?
- Empty tax_query array returns an empty array
- How do I search inside specific taxonomies in WordPress
- What’s the purpose / logic of split_the_query in WP_Query->get_posts
- WP_query posts closest to todays date
- Setting get_queried_object
- remove query arg from url after set query
- difference between like ‘%%%var%%’ and ‘%var%’
- Sort query by author: 1 author, then others
- Using hook to use DISTINCT in a wp_query
- Overwrite YoastSEO meta-tags with another page’s [closed]
- How to orderby multiple meta fields if some fields are empty
- Filtering posts from different categories into different section by doing WP_Query only once
- How to duplicate 5 posts out of WP_Query results?
- Where to put meta Keys
- WP_query sorting can’t sort danish letters (æ, ø, å)
- Slow queries on a huge database
- Large AND OR query timing out
- Multisite pagination issue by multi query archive 404
- Query with relation and one without relation using multiple taxonomies?
- get_post_meta slowing down my page load (in a plugin)
- Custom query: Get all posts that are from a certain category?
- Transient api Caches confused
- How can I override one post and make it display content for another post?
- Ajax future single post query doesn’t work when NOT logged in
- Conditional sorting with variable using WP Query
- Get multiple users with meta value in one query and populate WP_User class
- Get a list of posts by specific category
- How to check the array values, what WP_Query has brought to me?
- How to add custom meta to ‘pre_get_terms’?
- How do I check if an article is popular this week?
- Shortcode for custom query not returning results when attributes are added
- Should I reset $wp_query?
- Get specific ACF key and value from all posts – no access to DB
- Display post meta conditionally based on it’s value
- Sort by custom field that is an array?
- I am officially missing something about transient posts
- Start Query from 2nd Post without offset
- Display upcoming events in the next 30 days by querying posts with timestamp in custom meta
- duplicated posts when using pagination
- Sort by price wpdb
- Trouble with serialized metadata
- Custom filter from post meta
- How to query users by post count no less than 10
- WP_Query about meta_key and its array value
- Create global array to compare inside a query
- I can’t get post based on its postmeta value and key
- Saving custom fields for WP_Query to retrieve
- Filter posts/pages by user_role array
- Optimising amount of calls to custom fields
- WP Query to order posts by multiple meta fields
- Search Query that Includes Custom Table
- Custom post types loop on a page template
- Parsing External Table Arguments
- difference between methods of query
- wpdb LIKE request shows all database data
- Search results stuck on page 1
- Use value from meta key array for use in WP_Query