Skip to content

Instantly share code, notes, and snippets.

@kcpt-steven-kohlmeyer
Created February 9, 2015 22:51
Show Gist options
  • Save kcpt-steven-kohlmeyer/55c60217b9223b577960 to your computer and use it in GitHub Desktop.
Save kcpt-steven-kohlmeyer/55c60217b9223b577960 to your computer and use it in GitHub Desktop.
Virtual URLs to modify query
<?php
function add_custom_query_vars($vars) {
$vars[] = "past_events";
return $vars;
}
// hook add_query_vars function into query_vars
add_filter('query_vars', 'add_custom_query_vars');
function custom_rewrite_rule() {
add_rewrite_rule('^past-events/?','index.php?post_type=events&past_events=1','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
function alterQuery($query) {
global $wp_query;
if ( ! is_admin() and $query->is_main_query() && is_post_type('events') ) {
/* Events */
$pastEvents = false;
if( isset( $wp_query->query_vars['past_events'] ) ) {
$pastEvents = $wp_query->query_vars['past_events'];
}
if( $pastEvents !== false ) {
$query->set( 'meta_query', array(
array(
'key' => 'end_time',
'value' => time(),
'compare' => '<'
)
) );
} else {
$query->set( 'meta_query', array(
array(
'key' => 'end_time',
'value' => time(),
'compare' => '>='
)
) );
}
}
}
add_action('pre_get_posts','alterQuery');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment