您可以尝试以下操作(未测试的查询):
function counttheposts_func( $atts ) {
/**
* Defaults
*
* @var array
*/
$args = shortcode_atts( array(
'posts_per_page' => -1,
'post_type' => 'deal',
'post_status' => 'publish',
'meta_key' => 'wpcf-warranty',
'meta_value' => '1',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'dealership-tax',
'field' => 'slug',
'terms' => array( 'grimsby' ), // The slug should be something like the result of sanitize_title().
),
),
), $atts );
/**
* If the shortcode contains the 'terms' attribute, then we should override
* the 'tax_query' since we have to treat this differently.
*/
if ( isset( $atts['terms'] ) && '' !== $atts['terms'] ) {
/**
* You can even try multiple term slugs separating them with ",". You'll
* probably want to escape each term with sanitize_title() or something to
* prevent empty results, since expects a slug, not a title.
*
* @var array
*/
$terms = explode( ',', $atts['terms'] );
$args['tax_query'] = array(
'relation' => 'AND',
array(
'taxonomy' => 'dealership-tax',
'field' => 'slug',
'terms' => $terms,
),
);
}
$posts_query = new WP_Query( $args );
return $posts_query->post_count;
}
测试的原始结果
$args
如果简称为like,则为变量
[counttheposts terms="term1,term2" meta_key="my-meta-key"]
Array
(
[posts_per_page] => -1
[post_type] => deal
[post_status] => publish
[meta_key] => my-meta-key
[meta_value] => 1
[tax_query] => Array
(
[relation] => AND
[0] => Array
(
[taxonomy] => dealership-tax
[field] => slug
[terms] => Array
(
[0] => term1
[1] => term2
)
)
)
)