How to filter custom posts by tags and custom fields?

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.

Leave a Comment