Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Set relation to be OR instead of AND in a custom Query WordPress

  • SOLVED

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');

Answers (2)

2021-12-23

Hariprasad Vijayan answers:

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')


Alvaro Rosado comments:

I want my search to find at least one field, I think the default is AND so my searches are looking for post that contains all of the fields with the info I'm searching for.

I want to be able to search post with at least one field correct, that's why I'm trying to set the relation as 'OR'

I did try your code but it didn't work :(


Alvaro Rosado comments:

I'm still getting this:
https://ibb.co/fN0Sz2q

If I change the age to 23 then:
https://ibb.co/71HXLY8


Hariprasad Vijayan comments:

Ok, It's hard to suggest without testing code. Try 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
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');


Alvaro Rosado comments:

I tried that and didn't work.

I upload the site here:
https://testing.chkndo.com/

user:
alvaro

pass:
N*rmeD@$2R3zTvUPxCP#@!RM

the file this one:
https://testing.chkndo.com/wp-admin/theme-editor.php?file=functions.php&theme=revolution-child


Hariprasad Vijayan comments:

The code were updating to wrong area. Pre get post won't work in your case. I have updated the code inside exp_post_slider_shortcode_victimas( $atts ), the shortcode callback. Now it work

Now you get result for following scenarios
https://testing.chkndo.com/victimas/?victim_name=Julio&victim_age=23
https://testing.chkndo.com/victimas/?victim_name=Julio&victim_age=24
https://testing.chkndo.com/victimas/?victim_name=Test&victim_age=23


Hariprasad Vijayan comments:

Important : Change your WP password. If in case, you can share credentials via email(which is in my profile).

2021-12-23

Arnav Joy answers:

Can you please explain what are you trying to get?


Alvaro Rosado comments:

I want my search to be able to find post that can contain at least one field of information instead of trying to find that all fields have the right info.

IE

I have post that have a field with age info that is 23
then the same post have a field with name info that is antonio

If I perform a search with the values "antonio and 24" I want to be able to get the result regardless that the age was not accurate.