Forums › Forums › Search & Filter Pro › Apply Filter onload
- This topic has 7 replies, 2 voices, and was last updated 7 years, 4 months ago by Trevor.
-
Anonymous(Private) June 13, 2017 at 3:33 pm #114750
Hi,
I need to apply a category filter onload rather than showing all categories. I’ve used the below code to set the default to ‘news’ and removed the all option. But it still loads all the posts rather than just the news post on load.
add_filter(‘sf_input_object_pre’, function ($input_object, $sfid)
{
if(($input_object[‘name’]!=’_sft_category’))
{
return $input_object;
}
$input_object[‘defaults’] = array(“news”);
array_shift($input_object[‘options’]);
return $input_object;
} , 10, 2);Trevor(Private) June 13, 2017 at 4:18 pm #114755You want to set it to default to news, but still want it selectable (the user can change in the form)? Do you want it always to having something selected?
If so, you would need to use this:
Where you would test IF category is set to anything, and if not, set it to News.
Anonymous(Private) June 20, 2017 at 12:42 pm #115847Thanks, How do i test if category is set? I’m using the code below but it always seems to return true, even when I click on one of the other category options.
if($sfid==100) { if(empty($query_args['tax_query'])) { $query_args['tax_query'] = [[ 'taxonomy' => 'category', 'field' => 'slug', 'terms' => 'news', ]]; } } return $query_args;
Anonymous(Private) June 20, 2017 at 4:56 pm #115929Hi, this is what I get onload. I’m not able to see the query if i click on one of the options because its using ajax, So i don’t know what the query_args look like when a filter is applied.
array (size=10) 'paged' => int 1 'search_filter_id' => string '100' (length=3) 'search_filter_override' => boolean false 'posts_per_page' => int 12 'post_status' => array (size=1) 0 => string 'publish' (length=7) 'meta_query' => array (size=0) empty 'post_type' => string 'post' (length=4) 'is_search' => boolean true 'is_archive' => boolean true 'orderby' => array (size=2) 'date' => string 'DESC' (length=4) 'title' => string 'DESC' (length=4)
Trevor(Private) June 21, 2017 at 1:35 pm #116123Whilst it isn’t exactly what you are doing, it might help, becasue I don’t think you are getting the right data/array. Try reading this thread:
https://support.searchandfilter.com/forums/topic/how-to-show-the-currently-selected-title-only/
-
AuthorPosts