How do I set 'relation' => 'OR' in this code:
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
// only modify queries for 'event' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'victima' ) {
// allow the url to alter the query
if( isset($_GET['victim_age']) ) {
$query->set('meta_key', 'victim_age');
$query->set('meta_value', $_GET['victim_age']);
}
// allow the url to alter the query
else if( isset($_GET['victim_name']) ) {
$query->set('meta_key', 'victim_name');
$query->set('meta_value', $_GET['victim_name']);
$query->set('meta_compare', 'REGEXP' );
}
}
// return
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');
Solution - 1
Hi, I am not 100% sure i understand the question, but i guess you can try something like this
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
// only modify queries for 'event' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'victima' ) {
// allow the url to alter the query
if( isset($_GET['victim_age']) ) {
$meta_query = array(
'key' => 'victim_age',
'value' => $_GET['victim_age'],
);
}
// allow the url to alter the query
else if( isset($_GET['victim_name']) ) {
$meta_query = array(
'key' => 'victim_name',
'value' => $_GET['victim_name'],
'compare' => 'REGEXP',
);
}
}
$query->set( 'meta_query', array('relation' => 'OR',
$meta_query
) );
// return
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts')
Solution - 2
Can you please explain what are you trying to get?
|