/**
* Get the author post count for a tax query.
*
* @link http://wordpress.stackexchange.com/q/159160/1685
*
* @param array $tax_query
* @return int
*/
function wpse_159160_get_author_posts_by_tax( $tax_query ) {
global $wpdb;
$where = get_posts_by_author_sql( \'post\', true, get_post()->post_author );
$tax_query = new WP_Tax_Query( $tax_query );
$sql = $tax_query->get_sql( $wpdb->posts, \'ID\' );
return ( int ) $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts {$sql[\'join\']} $where {$sql[\'where\']}" );
}
使用中:
$count = wpse_159160_get_author_posts_by_tax(
array(
array(
\'taxonomy\' => \'highlight\',
\'terms\' => \'featured\',
\'field\' => \'slug\',
)
)
);
echo "$count Featured Posts";
如果希望减少对函数的控制(&;neater模板代码),可以硬编码
$tax_query
函数中的参数,只需调用它而不在其他地方使用参数。