前投稿にあったのをちょっと改造してこんな感じでどうでしょうか?
function ps_number_cat( $post_type = 'post', $op = '<=' ,$cat_id = '1') {
global $wpdb, $post;
$categories = get_the_category($post->ID);
$in_cat=0;
foreach($categories as $category){if($category->term_id == $cat_id){$in_cat=1;}}
if($in_cat){
$post_type = is_array($post_type) ? implode("','", $post_type) : $post_type;
$number = $wpdb->get_var("
SELECT COUNT( * )
FROM $wpdb->posts as p
LEFT JOIN $wpdb->term_relationships as r ON p.ID = r.object_ID
LEFT JOIN $wpdb->term_taxonomy as t ON r.term_taxonomy_id = t.term_taxonomy_id
LEFT JOIN $wpdb->terms as terms ON t.term_id = terms.term_id
WHERE post_date {$op} '{$post->post_date}'
AND post_status = 'publish'
AND post_type = ('{$post_type}')
AND t.taxonomy = 'category' AND terms.term_id = '{$cat_id}'
");
return $number;
}else{
return "";
}
}
$cat_idに含まれたカテゴリーのIDの順番を数えて、そのカテゴリーに属していない投稿に対しては空を返します。
トピック投稿者
camera
(@seesawcamera)
gogowebさん
ありがとうございます!
上記の方法で解決できました。
大変助かりました!