正如@Robert hue所说,你不能直接做到这一点,你需要一个过滤器和一个CASE
在orderby eg中
$args1 = array (
\'orderby\' => \'tag_count\',
\'order\' => \'DESC\',
\'post_type\' => \'post\',
\'pagination\' => true,
\'posts_per_page\' => \'1\', // 1 post per page?!
\'meta_key\' => \'_thumbnail_id\',
\'suppress_filters\' => false,
);
function wpse173949_posts_clauses( $pieces, $query ) {
if ( $query->get( \'orderby\' ) != \'tag_count\' ) return $pieces;
global $wpdb;
if ( ! ( $order = $query->get( \'order\' ) ) ) $order = \'DESC\';
$pieces[ \'fields\' ] .= $wpdb->prepare(
\', (SELECT COUNT(tr.object_id) FROM \' . $wpdb->term_relationships . \' tr\'
. \' JOIN \' . $wpdb->term_taxonomy . \' AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id\'
. \' WHERE tr.object_id = \' . $wpdb->posts . \'.ID AND tt.taxonomy = %s) AS tag_count\'
, \'post_tag\' );
// Treat zero tagged posts as max int (~0).
$pieces[ \'orderby\' ] = \'CASE WHEN tag_count THEN tag_count ELSE ~0 END \' . $order;
return $pieces;
}
add_filter( \'posts_clauses\', \'wpse173949_posts_clauses\', 10, 2 );
$query = new WP_Query( $args1 );
remove_filter( \'posts_clauses\', \'wpse173949_posts_clauses\', 10 );