Forums › Forums › Search & Filter Pro › Load defaults on page load
- This topic has 9 replies, 2 voices, and was last updated 8 years, 7 months ago by Anonymous.
-
Anonymous(Private) January 28, 2016 at 3:53 pm #35588
Hi, lets say i have different pages: dogs, cats and snakes.
When i open the dogs site the searchform should only load all dogs results.
Could this be done?
I want to insert the search form into a theme template. The data (dogs, cats, snakes) are stored in custom fields.
It should work like: If the current page has as custom field “dogs”, load only dogs results.
Thx.
Ross Moderator(Private) January 28, 2016 at 4:19 pm #35589Hey Christian
Can you explain in more details. You are using Dog page and dog site interchangeably.
Can you be specific what kind of setup you want this to occur on?
It is possible to do something similar, but this is only on tag/category/taxonomy archives.
Thanks
Ross Moderator(Private) February 15, 2016 at 11:15 pm #37039Hey Christian
Sorry for the delay.
I see what you mean, you want it so that when you “detect defaults” it also applies that criteria to the search results too?
I’m thinking of adding this as an additional setting.
Thanks
Ross Moderator(Private) April 6, 2016 at 8:48 am #41753Hi Christian
I have not yet had chance to implement this feature – as mentioned it only works with archives at the moment – I have just realised though if you are a dev and can do some coding this may already be possible using a filter:
So in there you would manually check to see if the current page/post has a specific category:
https://codex.wordpress.org/Function_Reference/has_category
if(has_category( 'dogs', get_the_ID())) { }
And if so, modify the S&F query to only look inside that category
$query_args['category_name'] = "dogs"
So putting it all together:
function update_search_category( $query_args, $sfid ) { //if search form ID = 225, the do something with this query if($sfid==225) { //here we check to see value is associated with the page if(has_category( 'dogs', get_the_ID())) { $query_args['category_name'] = "dogs" } else if(has_category( 'cats', get_the_ID())) { $query_args['category_name'] = "cats" } else if(has_category( 'snakes', get_the_ID())) { $query_args['category_name'] = "snakes" } } return $query_args; } add_filter( 'sf_edit_query_args', 'update_search_category', 10, 2 );
One big caveat I just realised, you may have to disable ajax – this is because an ajax request gets sent off somewhere else, and I think
has_category
will never give the correct result in an ajax request.Thanks
-
AuthorPosts