这并不能完全回答这个问题,但在处理更大的数据库时,这可能是一个更好的解决方案
// Get all posts IDs - Query took 0.0030 seconds
$all_posts_ids_query = $wpdb->prepare( "SELECT $wpdb->posts.ID FROM $wpdb->posts WHERE $wpdb->posts.post_type = \'%s\' GROUP BY $wpdb->posts.ID", \'your-post-type\' );
$all_posts_ids = $wpdb->get_col( $all_posts_ids_query );
// Get all posts IDs that has tags - Query took 0.0300 seconds
$all_posts_ids_with_tags_query = $wpdb->prepare( "SELECT $wpdb->posts.ID FROM $wpdb->posts
INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE 1=1 AND ( $wpdb->term_taxonomy.taxonomy = \'%s\' )
AND $wpdb->posts.post_type = \'%s\' GROUP BY $wpdb->posts.ID", \'your-taxonomy-name\', \'your-post-type\' );
$all_posts_ids_with_tags = $wpdb->get_col( $all_posts_ids_with_tags_query );
// Diff IDs arrays to get posts without tags
$all_posts_ids_without_tags = array_diff( $all_posts_ids, $all_posts_ids_with_tags );
测试的数据库大小(非常小,但可能有一些代表性),帖子:~ 3000条,术语:~ 1000条,术语关系:~ 6000条