Per comments, just use pre_get_posts
filter (no query_posts()
) very much as you have it with a few adjustments, eg test for $query->is_category( 'news' )
so that it only runs on news queries, and set the default to “2014” rather than bailing on no $_GET
, eg
function wpse129223_custom_meta_query( $query ) {
// If not on the main query, or on an admin page, bail here
if ( ! $query->is_main_query() || is_admin() || ! $query->is_category( 'news' ) ) {
return;
}
// Get the existing meta query, cast as array
$meta_query = (array) $query->get('meta_query');
// Convert 'type' into an array of types
$types = explode( ',', empty( $_GET['years'] ) ? '2014' : $_GET['years'] );
// Ensure that types aren't empty
if ( is_array( $types ) && ! empty( $types ) ) {
// Build a meta query for this type
$meta_query[] = array(
'key' => 'years',
'value' => $types,
'compare' => 'IN',
);
// Update the meta query
$query->set( 'meta_query', $meta_query );
}
}
add_action( 'pre_get_posts', 'wpse129223_custom_meta_query' );