Home › Forums › Pro Support › Query on more than one meta key or meta key with array value
Tagged: "Meta key", "Meta value", meta array
- This topic has 9 replies, 2 voices, and was last updated 2 years, 9 months ago by
Tom.
-
AuthorPosts
-
December 1, 2020 at 2:23 am #23486
Theo
ParticipantHi,
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
December 1, 2020 at 11:14 am #23548Tom
KeymasterHi 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 π
December 1, 2020 at 11:56 am #23554Theo
ParticipantHi 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
December 2, 2020 at 12:34 pm #23657Tom
KeymasterAgreed! No limits with that filter π
December 4, 2020 at 7:38 am #23764Theo
ParticipantHello 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.
December 4, 2020 at 2:14 pm #23788Tom
KeymasterWhere exactly are those variables coming from? The database? A query var in the URL?
December 4, 2020 at 3:51 pm #23792Theo
ParticipantI 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
December 7, 2020 at 3:19 pm #23929Tom
KeymasterIf the variables are only available via POST, then your method is good as long as the values are escaped π
December 7, 2020 at 3:51 pm #23937Theo
ParticipantThanks, Tom!
December 11, 2020 at 8:35 pm #24144Tom
KeymasterNo problem π
-
AuthorPosts
- You must be logged in to reply to this topic.