Don’t use $wpdb
, use wp_query
. Not only will your code be leaner and quicker, you’ll avoid issues like this. Unless you’re doing something with custom tables or seriously in-depth SQL, there’s no reason to do direct SQL calls on $wpdb
.
So, to answer your question – rewrite it from the ground up using wp_query
, see that everything you want to do is documented in the Codex, solve your problems without any hassle, and pat yourself on the back, a job well done.
Related Posts:
- Differences between wpdb->get_results() and wpdb->query()
- How to Use Wildcards in $wpdb Queries Using $wpdb->get_results & $wpdb->prepare?
- wpdb get posts by taxonomy SQL
- Multipart/formatted MySQL query problem
- Can’t pass table to $wpdb->prepare
- $wpdb->get_var not returning a result
- Most effective use of DB querys
- $wpdb->query() vs. $wpdb->get_results() vs. phpMyAdmin
- wpdb::prepare() isn’t working
- $wpdb doesn’t appear to work on page inside of a plugin
- Strange string in console from wpdb query
- Using $wpdb->update but confused on the WHERE in and SET
- What’s wrong with my $wpdb prepare?
- Make one query for adding entries to database
- How to execute a SQL-query which contains multiple queries using $wpdb->query?
- Alter a specific query on WordPress
- wpdb->term_taxonomy count posts from both published and private posts?
- wpdb print all post meta
- Plugin will sort users by usermeta
- wpdb query problem to access previous 3 days posts
- How to write: $wpdb->update having WHERE NOT value pair in the array
- I can’t figure out what’s wrong with this statement. $wpdb->query update
- Use mysql_query instead of $wpdb query
- wpdb COALESCE won’t work
- Custom Query – Based on user input
- Custom SQL Query passing array to WHERE p.id
- Get posts from category with custom query
- Custom Query returns array, but I can’t see it’s elements
- Valid SQL query return empty
- mysql query – how to escape apostrophe?
- $wpdb->prepare affecting the query?
- prepare function sql safe method
- Updating with $qpdb->query() always returns 0 rows affected
- How to return count of custom post type posts with a specific custom field value via $wpdb?
- WordPress Mysql query and Duplicate
- How do I query for posts by custom meta and those that have been stickied?
- Help with Related Posts Function
- wpdb update multiple row?
- Formatting a date/time returned from a custom $wpdb query
- Improve performance of slow query
- How do I get specific readable results from this query and array results
- implementing my wp_query using $wpdb class
- Exclude Posts from a Widget
- Fastest and most efficient SQL query to check if UID exists
- Use $wpdb->get_results with filter based on array
- $wpdb query for price in custom field value
- Display result from custom post meta query
- Wpdb empty result only if language is Russian
- Run an update query in a function
- How to fix this insert? Don’t work for me!
- Problem on wpdb queries, wp_insert_post, wp_insert_user etc. The query runs twice
- Update vs Insert logic but the last key is always inserted?
- There is way to improve this query? wordpress get post data and some meta data
- $wpdb: how to use a query as a base for another query?
- Wpdb generates too many queries
- Custom Query for searching through custom fields
- Mysql query and odd results
- How to reference same column name but different table in custom db query
- Retrieving custom field array value through db query
- How to get one result using wpdb class?
- Delete query won’t run
- update $wpdb one query
- Custom query_var causes displaying posts archive on front page
- What is the most efficient way of implementing a favorite post system?
- Using $wpdb to query posts with meta value containing current post_id
- Why does get_the_time(‘F j’) return November 30 for all posts?
- Query by one meta_key and sort by another (possibly NULL value)
- WPDB Multiple site’s posts and get featured images
- Query & Sort Comments by custom comment meta
- How to get category link without a database query
- Inserting data into `post meta` table?
- search through post-type attachments titles
- mysql query paging
- How to set back query var author_name after unset it?
- My SQL function to change user_name
- WordPress Loop: List All Posts by a Category & Subcategory
- Order query by post meta value
- List all posts from past week grouped by sub-category
- Query Custom Post Type posts by Keyword/url slug
- how would I create a custom query to get all users, and a related post based on a postmeta field?
- Display posts with a start OR end date later than current date
- Query to Get All Users from wp_users With NO matching user_id In wp_usermeta
- WordPress custom setting page – add flashing message
- Multiple Category Query
- wp_list_pages() refuses to output posts
- Can’t seem to do combined query AND sort?
- sort query results by newest
- Best practice to limit results in get_row()?
- Ideas how to search & replace post_content when string contains a newline?
- WordPress SQL search, how to handle SQL Injection?
- Query to view scheduled or draft post
- WP MS: How to query over the network
- How to add a Variable to post query
- Sort a list of a CPT ( job) if a meta is empty
- Restore WordPress Backup Locally?
- Trouble migrating custom post types from non-wordpress cms
- “order” does not affect order of custom query
- Run search query again without pagination gives no results?
- Where can I find the SQL to get the most used information by wordpress database?
- WooCommerce Total # orders [closed]