WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 2 down vote accepted
$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.

share|improve this answer
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 '12 at 5:08
count( $posts ) and setup_postdata‌​, respectively. Alternately, you can use WP_Query args in query_posts() – mor7ifer Feb 1 '12 at 18:44
$count = $posts->found_posts; // worked for the count – Jay West Feb 1 '12 at 19:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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