Horje
Set relation to be OR instead of AND in a custom Query - Wordpress Solution
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?





Wordpress

Related
How to show only parent terms with get_the_term_list() shortcode - Wordpress Solution How to show only parent terms with get_the_term_list() shortcode - Wordpress Solution
woocommerce order splitting - Wordpress Solution woocommerce order splitting - Wordpress Solution
Display post loop associated with current category url slug - Wordpress Solution Display post loop associated with current category url slug - Wordpress Solution
L.S. I'am developing an website for a local photoclub, i using Nextgen - Wordpress Solution L.S. I'am developing an website for a local photoclub, i using Nextgen - Wordpress Solution
wp query with multiple of same meta keys - Wordpress Solution wp query with multiple of same meta keys - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
32