I would use the built in API like Rarst mentioned. You could do something like this:
$just_seven = new WP_Query(
array(
'category__in' => array( 7 ),
'category__not_in' => array( 10 )
)
);
You would then have those items in $just_seven->posts
.
However, if you MUST use a direct SQL statement, I’d suggest using INNER JOIN
instead of LEFT JOIN
.
Related Posts:
- simple sql query on wp_postmeta very slow
- How many WordPress SQL Queries per page?
- Multipart/formatted MySQL query problem
- Are database queries created using WordPress filters protected from SQL injection?
- Grouping related postmeta data via SQL query
- Mysql / WordPress killing my server with 80k users [closed]
- Slow meta_query with about 4 milion record on wp_postmeta
- Select User by Joining Multiple Meta Value Results
- mySQL query. ORDER BY meta_key
- Update slug (URL) of pending posts via phpMyAdmin
- My SQL function to change user_name
- You have an error in your SQL syntax – Help with query
- MySQL variable in query
- how would I create a custom query to get all users, and a related post based on a postmeta field?
- Help with MySQL to $WPDB query
- How do I see the mysql query generated by get_posts( $args )?
- Alter query with posts_clauses to retrieve NULL values last
- MySql Query very slow
- Searching With Apostrophe
- WordPress Query wp-terms SLOW
- wpdb COALESCE won’t work
- Get posts from category with custom query
- how to insert missing tags into the posts through mySQL?
- Ideas how to search & replace post_content when string contains a newline?
- Valid SQL query return empty
- mysql query – how to escape apostrophe?
- Query the WordPress database to get data together with replaced information
- Optimize slow SQL query for multiple meta values
- 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
- Help with Related Posts Function
- How to delete all images from code in all post_content
- I want to get on those users their meta value are like “AGENT” .. but this query is not working
- Displaying data from custom table
- Get 2 meta values from meta key column
- PHP Fatal error: Uncaught Error: Call to a member function insert() on null
- SQL query to delete users with multiple meta keys and comments
- OR condition not working
- Trouble migrating custom post types from non-wordpress cms
- Are there any best practices for creating a Like/Favourite feature in WordPress using custom MySQL tables and without any plugins?
- Performance of wp_get_attachment_image_srcset() and wp_get_attachment_image_url()
- How to make MySQL search queries with quotes
- Custom MySQL Query for Post and Post Meta
- Mysql query and odd results
- sql for querying post and their category
- Access Tables with number prefix
- Get all image from single page using this query
- Search and replace text across all posts
- wp_dropdown_categories with multiple select
- How to tune search argument in WP_Query to show only exactly the same results?
- How to get my loop to pull posts into three columns
- Custom $wpdb Query for Custom Post Type by Category
- Can’t pass table to $wpdb->prepare
- Date query year and month OR just year
- How to speed up wp_query, took more 5s to run against 100k posts
- get post id using the $query_vars variable
- Calculating Bayesian average for custom post type
- Is there a conditional tag for latest post or do i need a query?
- get_posts() loop returns the same the_title() for each post
- Disallow categories from this MySQL query
- How to check current user in meta value array in WP_Query meta_query
- How select a specific query when setting offset?
- Change media attachment author via mysql query
- Query based on title, with ‘compare’ => ‘IN’
- Issue with using add_rewrite_rule() for post querying custom fields
- Plugin will sort users by usermeta
- Query posts according to specific post meta values
- empty query breaks other queries
- Problem in exploding the_content to array
- What is wrong with my wp query, cant exclude the category that i want
- SQL query to set posts in bulk based on the post content
- How to write update query in WordPress to expire transients
- Query Nickname rather than Display Name in custom Woocommerce plugin
- Get previous posts list
- how to replace old DW site with new WP site? [closed]
- WordPress SQL query to tag all posts containing a specific word on title
- Order terms by count – missing terms
- Querying Advanced Custom Fields
- How to rearrange a pool of posts joined using two queries?
- wpdb update multiple row?
- Can I include get_posts in this array?
- Custom query Custom-Post-Types creates error
- Formatting a date/time returned from a custom $wpdb query
- Rewrite rule for query params
- Hook into the Admin Dashboard and redirect users
- SEO friendly query vars
- Prevent versioning for .woff (font) files
- Orderby event date (if it exists) or post date (if it doesn’t) – how to reorder posts based on meta value OR date
- Query not returning child posts for any parent post ID
- how to get postmeta info based on multiple categories?
- Use $wpdb->get_results with filter based on array
- Add Hook for clearing transient when post is added
- Show links to child pages on both parent AND child pages
- Modify Global Posts Plugin
- How to reference same column name but different table in custom db query
- Retrieving custom field array value through db query
- Query Concatenation
- SQL Query : how copy all tags of post into their post content in wordpress by sql query
- How to figure out correct wp_options to have autoload=’yes’