• Resolved myCred

    (@designbymerovingi)


    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)
  • <?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

    (@designbymerovingi)

    Thank you for your help.

    But how do I implement a limitation towards a specific category?

    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

    (@designbymerovingi)

    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> ';
     	 	}
    
    	}
    }

    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

    Thread Starter myCred

    (@designbymerovingi)

    Thank you, that solved it.

    Working example is here.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Counting Specific Posts’ is closed to new replies.