我有一个自定义的post类型,它有许多分类法。在前端,用户可以为分类法(硬件或软件)分配“类别”(term\\u meta)值。
我想能够数一数有多少term\\u meta帖子。
$base_array = array(
\'posts_per_page\' => -1,
\'fields\' => \'ids\',
\'post_type\' => \'cpt\',
\'post_status\' => array(\'publish\'),
\'date_query\' => array(
\'before\' => \'next Saturday\',
\'after\' => \'last Monday\'
)
);
$base = get_posts($base_array);
echo count($base);
这将给我一周的帖子总数。但我想统计一下那些分类法中有“硬件”或“软件”这两个术语的帖子。
这可能吗?
最合适的回答,由SO网友:markb 整理而成
所以,看看我的其他代码,我觉得这是正确的方法。我还没有测试过它,但我觉得它也会以我需要的方式工作(不再需要计算类型)。
$base_array = array(
\'posts_per_page\' => -1,
\'fields\' => \'ids\',
\'post_type\' => \'cpt\',
\'post_status\' => array(\'publish\'),
\'date_query\' => array(
\'before\' => \'next Saturday\',
\'after\' => \'last Monday\'
)
);
$base = get_posts($base_array);
foreach( $base as $post_id ) {
$term_meta = get_term_meta( post_id, \'tax_term_type\', true );
$term_hardware += ($term_meta == \'term_meta_hardware\') ? 1 : 0;
$term_software += ($term_meta == \'term_meta_software\') ? 1 : 0;
}
echo count( $term_hardware );
echo count( $term_software );
SO网友:BlueSuiter
$base_array = array(
\'posts_per_page\' => -1,
\'fields\' => \'ids\',
\'post_type\' => \'cpt\',
\'post_status\' => array(\'publish\'),
\'date_query\' => array(
\'before\' => \'next Saturday\',
\'after\' => \'last Monday\'
),
\'tax_query\' => array(
\'taxonomy\' => \'pas_here_taxonomy\'
\'field\' => \'slug\',
\'terms\' => array( \'hardware\', \'software\'),
\'operator\' => \'IN\'
)
);
$base = get_posts($base_array);
echo count($base);