Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm planning a site for a client who wants users to be able to filter upcoming events by the following criteria:

Type i.e. Comedy, Theatre, Music etc.

Date Daily or Weekend

Duration Daily or Half day

I want to delve into Custom posts more, so will set up a Custom Post Type for 'Events' (non-hierarchical) and when creating a new post I'll add Custom Fields for Type, Date and Duration.

The question I have is how would I use query_posts() to:

  1. List the events so that the soonest (i.e. next event to occur) appears first and later events last?
  2. Filter by multiple meta values (Type, Date and Duration)?

I presume I need to create a series of options in my form's dropdown lists based on the custom field values right?

I've created a list of Events for a client before, but with no filtering possibilities. I used this query_posts() code to get the custom posts and order things:

// List the events by custom field 'Date':
$todaysDate = date('Y/m/d'); // set todays date to check against custom field

// query posts
query_posts('post_type=Event&meta_key=Date&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC');

However, I can't see how I might change this code to filter by multiple custom fields...any ideas?

Thanks

share|improve this question

1 Answer 1

up vote 4 down vote accepted

you can use custom taxonomies and make you query_posts much easier! by that i mean create a custom taxonomy for type,duration like so:

add_action('init','register_event_tax');

function register_event_tax(){
register_taxonomy('even_type',array('events'), array(
    'hierarchical' => false,
    'labels' => 'type',
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'type' ),
  ));

register_taxonomy('even_duration',array('events'), array(
    'hierarchical' => false,
    'labels' => 'type',
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'duration' ),
  ));
}

then you can query posts like this:

query_posts('post_type=Event&duration=DAILY&type=COMEDY&meta_key=Date&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC');

and you can change the duration and type to filter what ever type or duration you want.

hope this helps.

share|improve this answer
    
That is superb! Thank you for this! –  Osu Jan 17 '11 at 16:13
    
happy i could help! –  Bainternet Jan 17 '11 at 16:31
    
One more question as I'm thinking about the way the client will be using this. They are unlikely to use more than two terms for the Duration custom taxonomy (day or half-day), so is there a way of restricting the terms available? The benefit of using a custom field in this case is I can create a dropdown list with just the two options to choose from - I'm using Custom Field Template plugin by the way). I'm starting to think I might have to go back to using just custom fields...but would like to do a combination if possible? –  Osu Jan 17 '11 at 16:56
1  
that's the beauty of things with WordPress, you can display the taxonomy input as dropdown. read shibashake.com/wordpress-theme/… its just what you need and still keep the query possibilities. –  Bainternet Jan 17 '11 at 17:32

Your Answer

 
discard

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