Title: Counting Specific Posts
Last modified: August 19, 2016

---

# Counting Specific Posts

 *  Resolved [myCred](https://wordpress.org/support/users/designbymerovingi/)
 * (@designbymerovingi)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/counting-specific-posts/)
 * Anyone who can show me how I can count the amount of posts there are on my website
   that are:
 * 1. Published as Private
    2. Published with a specific tag (i.e. all posts with
   the tag “food”) and 3. Published with a specific post meta tag (i.e. one I can
   get with get(‘status’);)
 * Just want to count them and display the result.
 * Been searching around for days and not been able to find any answers. Would really
   appreciate any help.

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/counting-specific-posts/#post-1435891)
 *     ```
       <?php
       $num_posts = wp_count_posts( 'post' );
       echo '<p>Number of private posts: '.$num_posts->publish. '</p>';
   
       $tag = 'food';
       $taxonomy = 'post_tag';
       $term = get_term_by('name', $tag, $taxonomy);
       if ($term) {
         $tagposts = get_objects_in_term(array($term->term_id), $taxonomy);
       }
       if ($tagposts) {
         echo '<p>Number of posts with tag '.$tag .': '.count($tagposts). '</p>';
       }
   
       $myposts = null;
       $metakey = 'your_custom_field_key';
       $args=array(
         'meta_key' => $metakey,
         'post_type' => 'post',
         'post_status' => 'publish',
         'showposts' => -1,
         'caller_get_posts'=> 1
       );
       $myposts = get_posts($args);
       if ($myposts) {
         echo '<p>Number of posts with metakey '.$metakey .': '.count($myposts). '</p>';
       }
   
       ?>
       ```
   
 *  Thread Starter [myCred](https://wordpress.org/support/users/designbymerovingi/)
 * (@designbymerovingi)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/counting-specific-posts/#post-1435924)
 * Thank you for your help.
 * But how do I implement a limitation towards a specific category?
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/counting-specific-posts/#post-1435925)
 * Ah, new and possibly unclear requirements, but how about this.
 *     ```
       $tag = 'mycategory';
       $taxonomy = 'category';
       $term = get_term_by('name', $tag, $taxonomy);
       if ($term) {
         $tagposts = get_objects_in_term(array($term->term_id), $taxonomy);
       }
       if ($tagposts) {
         echo '<p>Number of posts with catgory '.$tag .': '.count($tagposts). '</p>';
       }
       ```
   
 *  Thread Starter [myCred](https://wordpress.org/support/users/designbymerovingi/)
 * (@designbymerovingi)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/counting-specific-posts/#post-1435927)
 * Thank you. Here is the result but I cant get it to search for multiple values.
 *     ```
       function count_in_factory_in_category($searchcat_id)
       {
       	global $post;
   
       	$factory_posts = null;
       	$metakey = 'status';
       	$args = array(
       	  'meta_key' => $metakey,
       	  'meta_value' => array('completed', 'in production', 'in development', 'archived'),
       	  'post_type' => 'post',
                 'post_status' => 'publish',
       	  'cat' => $searchcat_id,
       	  'showposts' => -1,
       	  'caller_get_posts'=> 1
       	);
   
       	$factory_posts = get_posts($args);
       	if ($factory_posts)
       	{
   
       	   $infactory_result = count($factory_posts);
       	   $category_url = get_category_link($searchcat_id);
   
       	   	if(is_user_logged_in())
       		{
       echo '	<div>
       			<p class="hits">';
   
       		/* if we have results that is more then 0 then insert the category link */
       		if($infactory_result != 0)
        		{
                       echo '<a href="'.$category_url.'" title="">'.$infactory_result.'</a>';
        		/* otherwise empty to hide this box */
        		} else { echo '&nbsp;'; }
       echo '		</p>
        		</div> ';
        	 	}
   
       	}
       }
       ```
   
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/counting-specific-posts/#post-1435933)
 * Try `'category__in'=> array($searchcat_id),`
 * And this will not work:
    `'meta_value' => array('completed', 'in production','
   in development', 'archived'),`
 * might want to look at [Displaying_Posts_Using_a_Custom_Select_Query](http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query)
 *  Thread Starter [myCred](https://wordpress.org/support/users/designbymerovingi/)
 * (@designbymerovingi)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/counting-specific-posts/#post-1435948)
 * Thank you, that solved it.
 * Working example [is here](http://www.merovingi.com/portfolio/).

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Counting Specific Posts’ is closed to new replies.

## Tags

 * [counting](https://wordpress.org/support/topic-tag/counting/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)
 * [tags](https://wordpress.org/support/topic-tag/tags/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 2 participants
 * Last reply from: [myCred](https://wordpress.org/support/users/designbymerovingi/)
 * Last activity: [16 years, 2 months ago](https://wordpress.org/support/topic/counting-specific-posts/#post-1435948)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
