Wordpress - allow custom fields as an URL variable
//Register alias public vars for meta field keys and values. From: http://wordpress.stackexchange.com/questions/22348
function register_public_vars() {
global $wp;
$wp->add_query_var( 'my_qv1' );
$wp->add_query_var( 'my_qv2' );
}
add_action( 'init', 'register_public_vars' );
//...And map them to query changes
function map_my_qv( $wp_query ) {
if ( is_admin() || ! $wp_query->is_main_query() )
return;
if ( $wp_query->get( 'my_qv1' ) ) {
$wp_query->set( 'meta_key', 'my_meta_key1' );
$wp_query->set( 'meta_value', $wp_query->get( 'my_qv1' ) );
}
if ( $wp_query->get( 'my_qv2' ) ) {
$wp_query->set( 'meta_key', 'my_meta_key2' );
$wp_query->set( 'meta_value', $wp_query->get( 'my_qv2' ) );
}
}
add_action( 'parse_query', 'map_my_qv' );
//We're now able to display posts that have specific custom fields! Don't forget post type URL argument: '?my_qv1=Canada&post_type=publication