Support

Account

Home Forums General Issues Count completed custom post types by a particular field.

Solving

Count completed custom post types by a particular field.

  • Hi There.
    I’ve got people filling in a ACF form which updates a custom post in wordpress.
    I’ll like to know how I can get a quick report of how many completed posts I have at any one time.

    I have a field on the form which required so I know that if that particular field is ticket then the form has been completed.

    Can someone please let me know the best way to do this?

    Any help would be appreciated.

    thanks

  • Do a query something like this

    
    $args = array(
      'post_type' => 'your-post-type',
      'post_per_page' => -1, // get them all
      'post_status' => 'publish', // or whatever status your looking for
      'meta_query' => array(
        array(
          'key' => 'your-field-name'
          'compare' => 'EXISTS',
        )
      )
    )
    $query = new WP_Query($args);
    $post_count = count($query->posts);
    
  • HI John

    Thanks for this…

    Very silly question but….

    Where would I put this bit of code?

    On a page?

    Thanks

  • Wherever it is that you want to see or use the number of posts. If you want to see the number you would do something like
    echo $post_count

  • Hi

    Thanks for this…
    I’ve created a new post type for this and added this into the post type.

    However I get this on the page…

    Parse error: syntax error, unexpected ”compare” (T_CONSTANT_ENCAPSED_STRING), expecting ‘)

    Can you help?

    thanks

  • There’s a missing comma in the code I provided. I don’t always test all the code I post because it’s meant as an example, sometimes typos slip in. I generally assume that most people can do basic code debugging.

    
    $args = array(
      'post_type' => 'your-post-type',
      'post_per_page' => -1, // get them all
      'post_status' => 'publish', // or whatever status your looking for
      'meta_query' => array(
        array(
          'key' => 'your-field-name',
          'compare' => 'EXISTS'
        )
      )
    )
    $query = new WP_Query($args);
    $post_count = count($query->posts);
    
  • Hi John

    Very sorry… I didn’t mean to offend.

    I’ve adjusted but it still doesn’t work.

    I appreciate any help offered.

    thanks

  • I don’t get offended, it’s all good, just explaining.

    just to be sure, did you change your-post-type to the post type you want to search for and your-field-name to the field name you’re looking for?

    Where are you trying to include the code?

  • I’ve created a single-reports.php file and I’ve added the code into that php file.

  • Can you post the code you’re actually using?

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

You must be logged in to reply to this topic.