0

I'm working on building a section for related posts in my theme's single.php file and I'm querying things based on the count returned from count (returned from get_the_category($post->ID);)

The only problem is that count is only including posts, and I have another post type called multimedia I want included in the count.

I've already added the following to my function.php file:

<?php 
//...

/**
 * Add multimedia / video reports to news listings
 */
function add_multimedia_to_tax( $query ) {
  if ( is_category() || is_tag() &&
       empty($query->query_vars['suppress_filters']) ) {

    // Get regular posts and mulimedia
    $post_types = array( 'post', 'multimedia' );

    $query->set( 'post_type', $post_types );

    return $query;
  }
}
add_filter( 'pre_get_posts', 'add_multimedia_to_tax' );

This makes multimedia show up in my category and tag archives as expected but the count is still off. For example, in one category I have 3 post and 2 multimedia articles, for a total of five articles but $categories[0]->count is still showing 3.

How can I go about this? I'd really rather not make a new query just to get a count. (If possible I'd like to have this in functions.php and change the category count across the entire site.)

Thank you!

0

You can use this code let me know if you are facing any issues in it.

$count_posts = wp_count_posts( 'jobs' )->publish;
  • How does that help get the number of posts in a particular taxonomy? That will just return all the posts for that CPT :/ – StephanieQ Sep 4 '17 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.