I am trying to count my custom post type called 'appointments' that match two meta data values .

The meta data keys are 'location' and 'timestamp'

So in english my query is: How many appointments are scheduled at Location at Time

How can this be done?

1 Answer 1

$args = array(
    'post_type'  => 'appointments',
    'meta_query' => array(
        'relationship' => 'AND',
            'key'   => 'location',
            'value' => $place //set this somewhere
            'key'   => 'timestamp',
            'value' => $time //set this somewhere
$posts = new WP_Query( $args );

Docs: WP_Query

You have the flexibility to add just about anything else you need to that, for example, post_status.

  • Thanks - That looks good, but how do I count it? I can run a $c++ in a loop, but I'd rather do it in the query if I can instead
    – Jay West
    Feb 1, 2012 at 5:08
  • count( $posts ) and setup_postdata, respectively. Alternately, you can use WP_Query args in query_posts()
    – mor7ifer
    Feb 1, 2012 at 18:44
  • 1
    $count = $posts->found_posts; // worked for the count
    – Jay West
    Feb 1, 2012 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.