Skip to content

Instantly share code, notes, and snippets.

@leocaseiro
Created July 18, 2014 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leocaseiro/afb4a380b3ba8bf6579b to your computer and use it in GitHub Desktop.
Save leocaseiro/afb4a380b3ba8bf6579b to your computer and use it in GitHub Desktop.
functions.php
<?php
function my_custom_queries($query) {
//Never in admin mode, only the main query(the URL one) and in this case on custom post 'event'
if ( !is_admin() && $query->is_main_query() && $query->is_post_type_archive('event')) {
$paged = ( get_query_var( 'page' ) ) ? absint( get_query_var( 'page' ) ) : 1; //URL /page
//For URLs like so:
// www.url.com/events/type/all/category/all/artists/all
//ignore the all var
$type = ( get_query_var( 'type' ) && 'all' != get_query_var( 'type' ) ) ? get_query_var( 'type' ) : null;
$category_name = ( get_query_var( 'category_name' ) && 'all' != get_query_var( 'category_name' ) ) ? get_query_var( 'category_name' ) : null;
$artists = ( get_query_var( 'artists' ) && 'all' != get_query_var( 'artists' ) ) ? get_query_var( 'artists' ) : null;
$tax_query = array();
//create filter for custom taxonomy type
if (!is_null($type)) {
$tax_query[] = array(
'taxonomy' => 'type',
'field' => 'slug',
'terms' => array($type)
);
} else {
//Force WordPress remove this filter when we pass all as paramester to the WP_Query
$query->query_vars['type'] = null;
}
//create filter for category
if (!is_null($category_name)) {
$tax_query[] = array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array($category_name)
);
} else {
//Force WordPress remove this filter when we pass all as paramester to the WP_Query
$query->query_vars['category_name'] = null;
}
//create filter for custom taxonomy type
if (!is_null($artists)) {
$tax_query[] = array(
'taxonomy' => 'artists',
'field' => 'slug',
'terms' => array($artists)
);
} else {
//Force WordPress remove this filter when we pass all as paramester to the WP_Query
$query->query_vars['artists'] = null;
}
if ( count($tax_query) > 1 ) {
//if more than 1 tax_query, let's put and AND parameter
$tax_query['relation'] = 'AND';
}
//Giving the new parameters to the main WP_Query
$query->tax_query->queries = $tax_query;
$query->query_vars['tax_query'] = $tax_query;
//Filtering using a custom date (Ymd) as a meta_query(custom field)
$today = date('Ymd');
$query->set('meta_query', array(
array(
'key' => 'custom_date',
'value' => $today,
'type' => 'numeric',
'compare' => '>=' //starting from today by a custom field
) //put more meta_query filters here if you need
));
$query->set('orderby', 'custom_date');
$query->set('order', 'ASC');
$query->set('paged', $paged); //to get from the URL or just the first page
//Make any filter you want using WP_Query with $query->set()
} /* Make a elseif() for other custom post archive or any page you need using the conditional tags */
}
add_action( 'pre_get_posts', 'my_custom_queries');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment