0

I made a custom post type, say ‘movie’, using "custom type post UI" plugin, and linked it with built-in category. Both normal posts and custom type posts are in the same category. My question is that if it is possible to modify the existing counting function so that it counts the number of posts that does not have a post type 'movie', i.e., only counts normal posts.

For example, one of my category, say ‘hobby’, has 5 normal posts + 3 custom ‘movie’ posts. Then, in the category widget, I want to have something like ‘hobby (5)’, not ‘hobby (8)’. In short, If it is possible, I just want to add a function (like add_filter or add_action or etc...) in functions.php to resolve this problem.

Thank you in advance.

  • Are you asking about just getting a fresh count of the posts or are you asking about modifying an existing count that's already output somewhere by WordPress. They're very different problems with different solutions. The current Answers are for the former, but I suspect you want an answer to the latter? – Jacob Peattie Feb 21 '18 at 8:47
  • @Jacob Peattie You are right. I want to modify the built-in counting function by WordPress. I checked "show post counts" in the category widget and it counts all posts (normal + custom) to show the number of posts. So I was wondering if there is a way to exclude custom posts when counting all posts in the category by modifying the existing counting function. Any suggestion? Thanks in advance. – jjycjn Feb 21 '18 at 14:28
0

One way to do it would be to run two custom queries like this:

$normal_post_args = array(
  'post_type' => 'post',
  'category_name' => 'hobby'
);
$normal_posts_query = new WP_Query( $normal_post_args );

$movie_post_args = array(
  'post_type' => 'movie',
  'category_name' => 'hobby'
);
$movie_posts_query = new WP_Query( $movie_post_args );


echo "Posts (" . $normal_posts_query->found_posts . ")";
echo "Movies (" . $movie_posts_query->found_posts . ")";

This should give you something like Posts (5) and Movies (3). That's a starting place. Then it's just a matter of wrapping this in a function and referencing it in your template file.


Update: Based on your comments, the best way I can think of to do this would be to create your own category list, rather than using the default WP one.

Something like this:

$categories = get_terms( 'category');
if($categories):
  echo "<ul>";

  foreach($categories as $cat):

    $cat_posts = get_posts('post_type=movie&category=' . $cat->term_id . '&numberposts=-1'); 
    if($cat_posts):
      echo "<li>" . $cat->name;
      $count = count($cat_posts); 
      echo " ($count)"; 
      echo "</li>";
    endif;

  endforeach;

  echo "</ul>";
endif;
  • I think my question was not so clear. As @Jacob Peattie mentioned in the comment, I want to modify the built-in counting function by WordPress. Do you also have a solution in this situation? – jjycjn Feb 21 '18 at 14:31
  • @jjycjn See above. Maybe this is more along the lines of what you're looking for? – dkeeling Feb 21 '18 at 18:11
  • It's working exactly as I wanted! Thank you so much. Now, I have to figure out how to add a link to each category and give css and etc... – jjycjn Feb 22 '18 at 14:01
  • Happy to help! You can use get_category_link() to create the link. Just pass in the ID of the category with $cat->term_id (in the foreach loop) – dkeeling Feb 22 '18 at 17:44
0

The answer by @dkeeling is valid, and working. However, if you are using WP_Query() you should also use wp_reset_postdata() afterward to reset the query and prevent messing with the other queries.

If you wish, you can use get_posts() instead, which returns the posts as an array that you can count using the native PHP count() function:

 // Get an array of posts
 $posts = get_posts ( 'numberposts=-1&post_type=movie&category_name=hobby' );

 // Count the posts
 $numbers = count ( $posts );

This way, you don't need an additional wp_reset_postdata().

Make sure you also set a numberposts or posts_per_page to retrieve all posts. Otherwise, the number of posts retrieved are limited to the default 5, or the number that you set in the Reading section of general options.

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.