My task was to make a list of all posts that were not processed by a specific plugin. The plugin was creating some postmeta entries that could help me to filter the ones that haven’t gone through the processing. I didn’t manage to come up with a query that could provide me with the said results, so I decided to use the lazy person’s aproach. I created a WP_Query instead and then used the Query Monitor plugin in order to extract the SQL query or group of queries that could be used for getting the final result.
Here are the query arguments:
$args = [ 'post_type' => 'post', 'post_status' => 'publish', 'meta_query' => [ [ 'key' => 'was_processed', 'compare' => 'NOT EXISTS' ] ] ];
This is the uneditted SQL query that WordPress generated as a result of this:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = 'was_processed' ) WHERE 1=1 AND ( wp_postmeta.post_id IS NULL ) AND wp_posts.post_type = 'post' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10