Support

Account

Home Forums Front-end Issues Count Custom Fields with Specific Value

Solving

Count Custom Fields with Specific Value

  • I have a custom field status which has 3 options unused, in progress and completed. I want to display how many posts with type of status. ex how many are marked completed. I am not sure how to do this. I do know some coding but not enough to figure this out on my own. Any help would be appreciated.

  • There is not an easy way to do this. You would need to do a query for each of the values and count the posts returned. For example

    
    // status completed
    $args = array(
      'post_type' => 'your post type',
      'post_status' => 'publish', // or whatever
      // other WP_Query arguments as needed
      'fields' => 'ids', // return only post IDs, since we just need to get a count
      'meta_query' => array(
        array(
          'key' => 'your field name',
          'value' => 'complete'
        )
      )
    );
    $results = new WP_Query($args);
    $completed = count($results->posts);
    
  • @aunrea — What type of field is your ‘status’? Radio buttons or?

  • @hube2 I tried that but results is not correct on all of them. I get a different number than it is supposed to be.

  • This works for me but kinda tiresome, can i do a loop for the different options since I’m using a selection list?

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

The topic ‘Count Custom Fields with Specific Value’ is closed to new replies.