• Resolved deckster0

    (@deckster0)


    How do you count all the posts in category X without the Y Custom field?

    Example:

    There are 15posts in the category named “Images”. Two of the posts (out of 15) have the custom field “Main Image”. How do I count the posts in the category that do not have the custom field “Main Image”?

    <?php echo ''. $wp_query->found_posts;?>
    ^Code for counting posts^

Viewing 2 replies - 1 through 2 (of 2 total)
  • MichaelH

    (@michaelh)

    Maybe not the fastest, but try:

    <?php
    $cat_id = get_cat_ID('Images');
    $args=array(
      'cat' => $cat_id,
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    $count = 0;
      while ($my_query->have_posts()) : $my_query->the_post();
        $image = get_post_meta($post->ID, 'Main Image', true);
        if (!$image){
          $count++;
        }
      endwhile;
    echo '<p>The count of posts in category Images without custom field Main Image is '.$count .'</p>';
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    Thread Starter deckster0

    (@deckster0)

    Works like a charm!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Count all posts in category without a certain custom field?’ is closed to new replies.