WordPress.org

Forums

getting total post count of custom post type in custom taxonomy? (3 posts)

  1. Brent
    Member
    Posted 9 months ago #

    Hi,

    I need to display something like this on my custom taxonomy archive page:
    Number of items: x, where x is the post count on the current page.

    I have 3 "categories" in my custom taxonomy and each of them have an individual "archive page"

    What code should I put in my taxonomy-taxonomyname.php file?

    Thanks in advance

  2. Brent
    Member
    Posted 9 months ago #

    More info:

    I'm displaying ALL posts on one page so x = total number of posts in that certain category.

  3. Bob Cristello
    Member
    Posted 9 months ago #

    You can try this generic function that I re-use in my projects. I hope this helps.

    <?php
    function get_custom_taxonomy_count($post_type, $tax_term, $taxonomy_name) 
    
    {
    
    	$taxonomy = 'my_taxonomy'; // this is the name of the taxonomy
    
        $args = array(
    
            'post_type' => $post_type, 'posts_per_page' => '1',
    
    		'meta_query' => array(
    
    				array(
    
    					'key' => 'closed',
    
    					'value' => '0',
    
    					'compare' => '='
    
    				)
    
    			),		
    
            'tax_query' => array(
    
                        array(
    
                            'taxonomy' => $taxonomy_name,
    
                            'field' => 'slug',
    
                            'terms' => $tax_term)
    
                    )
    
            );
    
         $my_query = new WP_Query( $args );
    
    	 return $my_query->found_posts;
    
    }
    
    ?>

Reply

You must log in to post.

About this Topic