Forums Forums Search & Filter Pro Apply Filter onload

Viewing 8 posts - 1 through 8 (of 8 total)
  • Anonymous
    #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
    #114755

    You 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:

    https://www.designsandcode.com/documentation/search-filter-pro/action-filter-reference/#Edit_Query_Arguments

    Where you would test IF category is set to anything, and if not, set it to News.

    Anonymous
    #115847

    Thanks, 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;
    Trevor
    #115899

    I would print the array out to see what data is in there when the category is and is not set. You should be able to output it using the php print_r() function inside <pre> tags.

    Anonymous
    #115929

    Hi, 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
    #115959

    If you are using ajax, then the php to test and show the filters should be inside the ajax container. But, for now, could you not switch off the ajax?

    Anonymous
    #116050

    I’ve removed the ajax and clicked all the options. The $query_args is the same for all three options. The only difference is the URL is now being updated “?_sft_category=blog” & “?_sft_category=news”

    Trevor
    #116123

    Whilst 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/

Viewing 8 posts - 1 through 8 (of 8 total)