Forums › Forums › Search & Filter Pro › Amending WP_Query for a form with a dynamic variable
- This topic has 8 replies, 4 voices, and was last updated 5 years, 5 months ago by Anonymous.
-
Anonymous(Private) June 9, 2019 at 4:24 pm #213610
Hi there,
I have search and filter form where I need to set a custom meta query based on the values held within a dynamic array variable based on the user who is logged in (which controls what they see).
So manually I tried the following:
Post Meta > post_product = 1134
Which is similar to what I want, and of course that works BUT I need the value of that query to be a dynamic array of product ID’s instead (held within $arr_product_ids in my example) depending on the user logged in, ie as follows:
'meta_query' => array( 'key' => 'post_product', 'value' => $arr_product_ids, 'compare' => 'IN', ),
With that in mind, I have tried: https://searchandfilter.com/documentation/search-results/custom/ and added the following to my functions.php file but sadly this has not worked:
global $arr_product_ids; $args = array( 'post_type' => 'post', 'posts_per_page' => 12, 'category_name' => 'news', 'meta_query' => array( 'relation' => 'AND', 'post_product' => array( 'key' => 'post_product', 'value' => $arr_product_ids, 'compare' => 'IN', ), 'post_urgent' => array( 'key' => 'post_urgent', 'compare' => 'EXISTS', ), 'post_priority' => array( 'key' => 'post_priority', 'compare' => 'EXISTS', ), ), 'orderby' => array( 'post_priority' => 'DESC', 'post_urgent' => 'DESC', 'date' => 'DESC' ), ); $args['search_filter_id'] = 2978; $query = new WP_Query($args);
I then looked at https://searchandfilter.com/documentation/action-filter-reference/ and tried adding the following to functions.php, but sadly again no luck:
function filter_function_name( $query_args, $sfid ) { if($sfid==2978) { $query_args['post_product'] = $arr_product_ids; } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
Please could you ket me know where I’m going wrong with this as I think I’m going around in circles sadly, whereas you will know the issue immediately I’m sure :).
Many thanks,
TJAnonymous(Private) June 9, 2019 at 9:44 pm #213626Hi TJ,
On your second example, did you try adding:
global $arr_product_ids
within the function too? Your trying to use $arr_product_ids variable.function filter_function_name( $query_args, $sfid ) { if($sfid==2978) { global $arr_product_ids; $query_args['post_product'] = $arr_product_ids; } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
Anonymous(Private) June 9, 2019 at 9:47 pm #213628TJ, another quick example maybe to help:
function filter_function_name( $query_args, $sfid ) { if($sfid==2978) { global $arr_product_ids; $query_args['tax_query'] = array( array( 'key' => 'post_product', 'value' => $arr_product_ids, 'compare' => 'IN', ), ); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
Ross Moderator(Private) June 11, 2019 at 6:51 pm #213829Hi Holly
I tried your code and it didn’t affect my setup at all (it should show no results, as that taxonomy doesn’t exist in my setup…)
So… I had to double check the WP docs as it seemed like your tax query was ok on first glance but its not.
I tried your code swapping out taxonomies for something I had in a testing environment and it didn’t work either (its much the same as yours):
$query_args['tax_query'] = array( array( 'key' => 'cptuicat', 'value' => array(9), 'compare' => 'IN', ) );
I checked the docs and actually it should look like I think:
$query_args['tax_query'] = array( 'relation' => 'AND', array( 'taxonomy' => 'cptuicat', 'field' => 'id', 'terms' => array( 9 ), ), );
This worked for me, the ID of
9
being a taxonomy term I have…If you copy and paste that, and swap out what you need then I think it should work.
Let me know how you get on.
Thanks
Ross Moderator(Private) June 11, 2019 at 6:53 pm #213831Actually looking at your code, it looks like your structure is for a
meta_query
not atax_query
🙂 -
AuthorPosts