Forums › Forums › Search & Filter Pro › Only return results which match specific post meta data
- This topic has 9 replies, 3 voices, and was last updated 7 years, 1 month ago by Ross.
-
Trevor(Private) October 10, 2017 at 4:45 pm #135753
Not in the Post Meta tab, you you should be able to make something that would work with your own code using this filter:
If you search for
sf_edit_query_args
in the forum, you will also find some posts showing how other users have used the filter, I think.Anonymous(Private) October 17, 2017 at 11:59 am #136903Thanks for that, I’ve got it working now but the count on the filters are wrong. The list of filters is now including all posts rather than the ones that match the new query?
This wasn’t a problem when I used the Post Meta conditions through the plugin UI
This is the function I have up to now…
function filter_upcoming_events( $query_args, $sfid ) { if($sfid==29642) { $query_args = array( "meta_query" => array( "key" => "event_date_time", "value" => time() - (60 * 60 * 2), "compare" => ">=" ), "meta_key" => "event_date_time", "order" => "ASC", "orderby" => "meta_value", ); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_upcoming_events', 20, 2 );
Page is at http://www.our-pub.co.uk/wp
As you can see, the England games under the Football filters have already been played so the England filter should be hidden as it’s empty. ticking the England box and submitting the search returns no results.
Thanks in advance.
Ross Moderator(Private) October 25, 2017 at 11:50 am #138398Hi there
Just taking a look at this.
The reason we implemented the
edit_query_args
filter is exactly for this reason, the arguments get passed through to our count functions too, so these should be updated, and its why this filter should be used over the WPpre_get_posts
…So, this is odd.
2 things I would suggest:
1) We are only trying to edit the
$query_args
(more specifically the meta query), not replace all of them (by using=
on the variable you replace anything in the array already), perhaps this is causing an issue. I would change your code above to:function filter_upcoming_events( $query_args, $sfid ) { if($sfid==29642) { $query_args["meta_query"] = array( "key" => "event_date_time", "value" => time() - (60 * 60 * 2), "compare" => ">=" ), "meta_key" => "event_date_time", "order" => "ASC", "orderby" => "meta_value", ); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_upcoming_events', 20, 2 );
I would also make sure, that any Post Meta settings, defined in the settings form, are removed / disabled, as well as the order results option in
posts
tab in your search form (this is all just to be safe and ensure no conflicts).2) You could have a
pre_get_posts
somewhere else in your setup, causing this unusual behaviour, but if using the post meta settings in the form works fine (with the counts) then this may not be the issue. Lets see about option 1 first.Let me know how you get on.
Thanks
-
AuthorPosts