Support

Account

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

Solving

Count Custom Fields with Specific Value

    • aunrea

    • February 17, 2018 at 8:26 am

    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?

    • aunrea

    • February 21, 2018 at 3:33 am

    It is a radio.

    • aunrea

    • March 7, 2018 at 3:14 am

    @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)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.