如何在WordPress中同时获取位于两个特定类别的帖子数量
我已尝试使用此代码:如何在WordPress中同时获取位于两个特定类别的帖子数量,wordpress,count,categories,posts,Wordpress,Count,Categories,Posts,我已尝试使用此代码: $terms = get_terms('translates_category', 'include=220,238'); 但它返回一个包含两个独立对象的数组: Array ( [0] => stdClass Object ( [term_id] => 220 [name] => Degrees of comparison [slug] => degrees-of-comparison
$terms = get_terms('translates_category', 'include=220,238');
但它返回一个包含两个独立对象的数组:
Array
(
[0] => stdClass Object
(
[term_id] => 220
[name] => Degrees of comparison
[slug] => degrees-of-comparison
[term_group] => 0
[term_taxonomy_id] => 272
[taxonomy] => translates_category
[description] =>
[parent] => 217
[count] => 2
)
[1] => stdClass Object
(
[term_id] => 238
[name] => Lesson
[slug] => lesson
[term_group] => 0
[term_taxonomy_id] => 290
[taxonomy] => translates_category
[description] =>
[parent] => 0
[count] => 1
)
)
我可以假设,它分别返回这两个类别中所有帖子的数量(count)。但我只需要同时位于这两个类别的帖子总数
第一类可能有100个职位,第二类可能有10个职位,但一次只能有1个职位与这两个类别相关。我需要统计这些职位
我怎样才能做到这一点呢?使用下面的代码,为此,您必须为帖子制作两部分,即每个部分一份
<?php
global $post;
$args = array( 'numberposts' => 5, 'category' => 3 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :
setup_postdata($post); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endforeach; ?>
更改要显示的帖子和类别的数量…您可以将此函数粘贴到
函数中。php
function get_post_count($categories) {
global $wpdb;
$post_count = 0;
$post_count_array=array();
foreach($categories as $cat) :
$catID=get_cat_id($cat);
$querystr = "SELECT count FROM $wpdb->term_taxonomy WHERE term_id = $catID";
$result = $wpdb->get_var($querystr);
$post_count += $result;
$post_count_array[$cat]=$result;
endforeach;
$post_count_array['total']=$post_count;
return $post_count_array;
}
然后像这样调用这个函数
$posts_Cat_Num=get_post_count(array('plugin', 'php')); // these are category names
print_r($posts_Cat_Num); // Array ( [plugin] => 2 [php] => 3 [total] => 5 )
echo $posts_Cat_Num['plugin']; // 2
echo $posts_Cat_Num['php']; // 3
echo $posts_Cat_Num['total']; // 5
更新(从评论中我理解了这个问题)
这将解决您的问题:
function my_post_count($tax, $cat1, $cat2) {
$args = array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => $tax,
'field' => 'term_taxonomy_id',
'terms' => array( $cat1 ),
'operator' => 'IN'
),
array(
'taxonomy' => $tax,
'field' => 'term_taxonomy_id',
'terms' => array( $cat2 ),
'operator' => 'IN'
),
)
);
$query = new WP_Query( $args );
return $query->post_count;
}
echo my_post_count('translates_category', 220, 238);
但我不需要发布帖子或分类。我需要获取位于两个类别中的帖子数量。您是否希望获取总数,因为cat1有3个,cat1有1个
3+1=4
?我需要获取当时与两个类别关联的帖子数量。第一类可能有100篇文章,第二类可能有10篇,但一次只能有1篇与这两个类别相关。这不是我真正需要的。我问过你,但现在我得到了你需要的。出于某种原因,它不会返回任何$q->posts中的文章。即使我指定了'tax\u query'=>array(array('taxonomy'=>'translations\u category'))@NikitaGavrilov,我已经在我的本地主机上测试过了,它对我很好,不管怎样,你已经接受了答案。谢谢你的帮助!看起来它真的很有效!我还没有完全掌握舒尔,但我会在这两个类别中用更多的帖子来测试它。谢谢!!!
function my_post_count($tax, $cat1, $cat2) {
$args = array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => $tax,
'field' => 'term_taxonomy_id',
'terms' => array( $cat1 ),
'operator' => 'IN'
),
array(
'taxonomy' => $tax,
'field' => 'term_taxonomy_id',
'terms' => array( $cat2 ),
'operator' => 'IN'
),
)
);
$query = new WP_Query( $args );
return $query->post_count;
}
echo my_post_count('translates_category', 220, 238);