Forums › Forums › Search & Filter Pro › sf_filter_query_args (Not working if I want to filter)
- This topic has 10 replies, 2 voices, and was last updated 7 years, 2 months ago by Trevor.
-
Anonymous(Private) August 24, 2017 at 12:27 pm #127398
Hello guys!
I have a custom sf_filter_query_args like so:
function sf_filter_query_args($query_args, $sfid) { if ($sfid == 327) { add_filter('posts_where', 'acf_posts_where'); $query_args['meta_query'] = array( 'relation' => 'AND', array( 'key' => 'likes_%_gebruiker', 'value' => '16', 'compare' => 'LIKE' ) ); } return $query_args; } add_filter('sf_edit_query_args', 'sf_filter_query_args', 10, 2);
Which works perfectly when I come on the page. It filters succesfully on the meta_query. But when I select a filter it returns all result and not the results where likes_%_gebruiker is 16 + the filters.
Am i doing something wrong? Can someone help me out?
Thanks
Trevor(Private) August 24, 2017 at 2:22 pm #127421You need to output the $query_args array before and after this code, like this:
echo '<pre>',print_r($query_args,true),'</pre>';
To see if any change was made. It may be that you need to run the code later, e.g.
add_filter('sf_edit_query_args', 'sf_filter_query_args', 10, 200);
Anonymous(Private) August 24, 2017 at 2:48 pm #127444Hi Trevor,
I made it so the code runs later and tested multiple values but I dont get it to work. See what is returns:
It only has to show the one with the red hearth, it works on page load but not when I select another filter (Speeltuin in example) (It works when I refresh the page with the filter on though, than it shows only the one with red hearth).
Any ideas?
Thanks!
Anonymous(Private) August 25, 2017 at 9:12 am #127591Yes it is, the code is as following:
function sf_filter_query_args($query_args, $sfid) { echo '<pre>',print_r($query_args,true),'</pre>'; if ($sfid == 327) { add_filter('posts_where', 'acf_posts_where_likes'); $query_args['meta_query'] = array( 'relation' => 'AND', array( 'key' => 'likes_%_gebruiker', 'value' => '16', 'compare' => 'LIKE' ) ); } echo '<pre>',print_r($query_args,true),'</pre>'; return $query_args; } add_filter('sf_edit_query_args', 'sf_filter_query_args', 10, 400);
Trevor(Private) August 25, 2017 at 10:34 am #127623What I was trying to establish was …
You arrive to the page and your code works. You show this with the two arrays shown in post 127457, where the fist array is empty, and then after your code has contents.
Then you make a filter selection, and the filter selection works, but it ignores your code.
So, when it does ignore your code, it should have run through your code, so on THAT page, what do the before and after arrays look like? Or do they not print out at all?
Anonymous(Private) August 28, 2017 at 1:49 pm #127918Sorry for my late answer. I think the problem is because it loads the posts using AJAX so it skips my array. If I select a filter it changed the URL using jQuery to https://url.com/favorieten/?_sfm_filter_locatie=rondom and shows all the “rondom” posts so it skips my array.
If I refresh that URL it works because than the posts does not get loaded using AJAX I guess?
-
AuthorPosts