[Resolved] Query on more than one meta key or meta key with array value

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Query on more than one meta key or meta key with array value

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #23486
    Theo
    Participant

    Hi,

    I’m using WPSP for a few weeks now and I’m struggeling with the following issues:

    1) In my site I’ve created a custom post type with a few meta keys in postmeta. Some of them have more than one value (e.g. cities), stored as an array (a:2:{i:0;s:9:”Gorinchem”;i:1;s:13:”Scharnegoutum”;}. Is it possible to query the array on a given value ( something like in_array() )? I’ve looked in te support formum, but couldn’t find an answer. I’m not using plugins like ACF, but use GeneratePress Pro as template.

    2) Is it possible to query on more than one meta key/value? I’ve create the following code, but would like to extend that with a few meta keys/values more.

    if ( function_exists( 'wpsp_display' ) ) { 
    	wpsp_display( 97,
    		'tax=' . $bd_taxonomy_to_use . '&tax_term=' . $bd_term_slug_list . '&meta_key=bd_post_average_rating&orderby=meta_value_num&order=DESC'
    	);
    }

    Kind regards,

    Theo

    #23548
    Tom
    Keymaster

    Hi there,

    For more complex queries, you need to do something like this: https://wordpress.stackexchange.com/a/237959/197564

    If you use our latest development version (https://wpshowposts.com/wp-show-posts-1-2-0/), you can filter the query:

    add_filter( 'wpsp_query_args', function( $args, $settings ) {
        if ( 123 === (int) $settings['list_id'] ) {
            $args['meta_query'] = array(
                array(
                    'key'     => 'post_code',
                    'value'   => '432C',
                ),
                array(
                    'key'     => 'location',
                    'value'   => 'XYZ',
                ),
            );
        }
    
        return $args;
    }, 10, 2 );

    Let me know if you need more info πŸ™‚

    #23554
    Theo
    Participant

    Hi Tom,

    Thank you for your quick reply! I’ll give it a try and let you know.

    Great that the new version of WPSP supports filtering queries! It makes this already great plugin so much more flexible…

    Regards,

    Theo

    #23657
    Tom
    Keymaster

    Agreed! No limits with that filter πŸ™‚

    #23764
    Theo
    Participant

    Hello Tom,

    Based on your suggestions, I’ve created a filter (see below, sorry for the lengty code here), and it works fine! Still some additions to make, though. E.g. I don’t like the way I’m passing variables right now. Via a function inside the filter I collect some $_POST data, but I can’t find onother a way to pass variables to the filter. As far as I know that’s not possible, or am I overlooking something? I’m a bit reluctant to pass global variables.

    // Filter query arguments for WP Show Posts 
    add_filter( 'wpsp_query_args', function( $args, $settings ) {
    	
    	// Declare variables
    	
    	$bd_location_key = $bd_location_value = '';
    	$bd_filter_data = [];
    
    	// Get location data ( passed via $_POST and already sanitized/escaped ) 
    	
    	$bd_filter_data = bd_get_location_post();
    	
    	// Assign location data to variables
    	// Because not all location data have to be passed, 
    	// we start at the top level (country), down to the most detailled level (city)
    	// Descending order: Country -> State -> County - City
    	
    	[ ... in my script, here are the conditionals tot assign value to $bd_location_key
    	and $bd_location_value. I've skipped them here ... ]
    
    	// Add query arguments to WP Show Posts
    	// Also create a custom order clause, so we can filter on
    	// the avarage rating, stored in post meta
    	
    	if ( 97 === (int) $settings[ 'list_id' ] ) {
    		$args[ 'meta_query' ] = array(
    			array(
    				'key'     	=> $bd_location_key,
    				'value'   	=> array_shift( $bd_location_value ),
    				'compare' 	=> 'LIKE',
    				'orderby' 	=> 'meta_value',
    				'order'   	=> 'ASC',										
    			),
    			'bd_rating_order_clause' => array(
    				'key'		=> 'bd_post_average_rating'
    			),
    		);
    		
    		$args[ 'orderby' ] = array(
    			'bd_rating_order_clause' => 'DESC'				
    		);
    	}
    
    	return $args;
    
    }, 10, 2 );

    I have added an extra argument to the filter and changed the number of arguments to 3, but that throws an error.

    #23788
    Tom
    Keymaster

    Where exactly are those variables coming from? The database? A query var in the URL?

    #23792
    Theo
    Participant

    I created a custom taxonomy and some post meta values, stored in the database. They are used for a taxonomy filter I am working on. The values are related to a custom post type (advertisement). When the user selects the filter option(s), a form is submitted (method is post, no ajax). I’m fetching the post variables in the filter via a function call.

    As said, your suggestion works fine and with the filter I can achieve what I want, but I have the feeling that it’s a makeshift solution I’ve createdπŸ˜‰.

    Regards,

    Theo

    #23929
    Tom
    Keymaster

    If the variables are only available via POST, then your method is good as long as the values are escaped πŸ™‚

    #23937
    Theo
    Participant

    Thanks, Tom!

    #24144
    Tom
    Keymaster

    No problem πŸ™‚

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.