MySQL JOINs explained with WordPress tables
WordPress spreads data over several tables: posts in wp_posts, their custom fields in wp_postmeta, categories and tags in three term tables. Almost every useful report needs a JOIN to combine them. The two you need most are INNER JOIN (only rows that have a match) and LEFT JOIN (all rows, with empty values where there is no match).
Problem: You need lists from several tables at once: posts with and without a featured image, posts with their category names, pages with their parent page, and you are not sure which JOIN gives which result.
Solution: Use INNER JOIN to keep only rows that have a match, LEFT JOIN to keep all rows of the first table, put conditions for the joined table into the ON clause, and group with GROUP_CONCAT when one row has several matches.
The examples use the wp_ prefix; replace it with yours.
INNER JOIN: only posts with a featured image
SELECT p.ID, p.post_title, pm.meta_value AS thumbnail_id FROM wp_posts p INNER JOIN wp_postmeta pm ON pm.post_id = p.ID AND pm.meta_key = '_thumbnail_id' WHERE p.post_type = 'post' AND p.post_status = 'publish';
LEFT JOIN: all posts, and which ones have no image
SELECT p.ID, p.post_title FROM wp_posts p LEFT JOIN wp_postmeta pm ON pm.post_id = p.ID AND pm.meta_key = '_thumbnail_id' WHERE p.post_type = 'post' AND p.post_status = 'publish' AND pm.meta_id IS NULL;
The meta_key condition is in the ON clause. In the WHERE clause it would remove the rows where the join found nothing, and the LEFT JOIN would silently behave like an INNER JOIN. That is the most common JOIN mistake.
Several JOINs: posts with their category names
SELECT p.ID, p.post_title, GROUP_CONCAT(t.name ORDER BY t.name SEPARATOR ', ') AS categories FROM wp_posts p LEFT JOIN wp_term_relationships tr ON tr.object_id = p.ID LEFT JOIN wp_term_taxonomy tt ON tt.term_taxonomy_id = tr.term_taxonomy_id AND tt.taxonomy = 'category' LEFT JOIN wp_terms t ON t.term_id = tt.term_id WHERE p.post_type = 'post' AND p.post_status = 'publish' GROUP BY p.ID, p.post_title ORDER BY p.ID DESC LIMIT 20;
A post with three categories produces three rows after the joins; GROUP_CONCAT with GROUP BY turns them back into one row with a list. Again, tt.taxonomy = 'category' is in the ON clause, so posts without categories, or with only tags, are still listed.
Self-join: pages and their parents
SELECT child.post_title AS page, parent.post_title AS parent_page FROM wp_posts child LEFT JOIN wp_posts parent ON parent.ID = child.post_parent WHERE child.post_type = 'page' AND child.post_status = 'publish' ORDER BY parent.post_title, child.menu_order;
The same table appears twice under two aliases. Top-level pages have post_parent = 0 and get NULL as the parent.
In PHP
<?php
global $wpdb;
$without_image = $wpdb->get_col( $wpdb->prepare(
"SELECT p.ID FROM {$wpdb->posts} p
LEFT JOIN {$wpdb->postmeta} pm ON pm.post_id = p.ID AND pm.meta_key = %s
WHERE p.post_type = 'post' AND p.post_status = 'publish' AND pm.meta_id IS NULL",
'_thumbnail_id'
) );
Use $wpdb->posts and $wpdb->postmeta instead of hard-coded table names, so the query works with any table prefix and in Multisite.
NOTE: JOINs on wp_postmeta are fast when they filter by post_id and meta_key, which are indexed. Conditions on meta_value are not indexed, and on a large table a query like “all posts where meta_value LIKE ‘%x%'” scans every row. Run such reports on a copy of the database, not on a busy live server.