Get queried object for custom post type count

$posts = get_queried_object();echo $posts->count;I can get post count with this code in taxonomy archive page . prob

$posts = get_queried_object();
echo $posts->count;

I can get post count with this code in taxonomy archive page . problem is that i can not specify post type. i just want count only A custom post types.

i tried to this:

$posts = get_queried_object(
$args = array(
  'post_type' => 'custom-post'
));
$the_query = new WP_Query( $args );
echo $the_query->found_posts;

but this code display total custom post type count

$posts = get_queried_object();
echo $posts->count;

I can get post count with this code in taxonomy archive page . problem is that i can not specify post type. i just want count only A custom post types.

i tried to this:

$posts = get_queried_object(
$args = array(
  'post_type' => 'custom-post'
));
$the_query = new WP_Query( $args );
echo $the_query->found_posts;

but this code display total custom post type count

Share Improve this question edited Jul 26, 2019 at 11:19 Gidromasservis QSC asked Jul 26, 2019 at 9:55 Gidromasservis QSCGidromasservis QSC 331 silver badge10 bronze badges 2
  • WordPress doesn't store this information, you'll need to perform a separate query for a specific post type in that term. – Jacob Peattie Commented Jul 26, 2019 at 10:01
  • now it showed total custom post count $posts = get_queried_object( $args = array( 'post_type' => 'CUSTOM-POST' )); $the_query = new WP_Query( $args ); echo $the_query->found_posts; – Gidromasservis QSC Commented Jul 26, 2019 at 11:14
Add a comment  | 

1 Answer 1

Reset to default 0
    function wpse340250_term_count( WP_Term $term, $post_type) {
    $q_args = [
        'post_type' => $post_type,
        'nopaging' => true, // no limit, pagination
        'fields' => 'ids', // only return post id's instead of full WP_Post objects will speed up
        'tax_query' => array(
            array(
                'taxonomy' => $term->taxonomy,
                'field'    => 'term_id',
                'terms'    => $term->term_id,
            ),
        ),
    ];
    $term_count = get_posts($q_args);

    return count($term_count);
}
<?php

// get the terms you need
$terms = get_terms( $posts = get_queried_object());
$sorted_terms = [];
if ( $terms ) {
    foreach ( $terms as $term ) {
        $sorted_term = [

            'count'              => (int) wpse340250_term_count( $term, 'Custom-post' ),
            // everything you will need later here
        ];

        // here handle all dynamic checks add them to $sorted_term
        if ( $term->taxonomy == 'category' && is_category() ) {
            $thisCat = get_category( get_query_var( 'cat' ), false );
            if ( $thisCat->term_id == $term->term_id ) {
                $sorted_term['carrentActiveClass'] = 'class="active-cat"';
            }
        }

        // for the sake of getting to the core of the answer I left stuff out.


        // add the complete sorted_term to the collection
        $sorted_terms[] = $sorted_term;
    }


}
// all done gathering data now we handle html output
?>
                       <?php echo $sorted_term['count'] ?>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745291590a4620892.html

相关推荐

  • Get queried object for custom post type count

    $posts = get_queried_object();echo $posts->count;I can get post count with this code in taxonomy archive page . prob

    14小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信