Search Results for 'sf_edit_query_args'

Forums › Forums › Search › Search Results for 'sf_edit_query_args'

Viewing 10 results – 191 through 200 (of 203 total)
  • Author
    Search Results
  • #41013

    Anonymous
    Inactive

    Hi,

    Is it possible to specify an array of posts ID’s to include in the returned search results?

    I used to use pre_get_posts to do this, modifying the ‘post__in’ query variable with my array, but it looks as though ‘post__in’ cannot be used anymore when filter a query?

    I have seen that since the development of the cache a few additional hooks have been introduced (such as sf_edit_query_args), but I’ve not be able to use ‘post__in’ with any of these (although I can use ‘p’ to specify a single post).

    Could you point me into the direction of how I could add extra post ID’s into my results?

    Thanks for your help 😀

    Matt


    Anonymous
    Inactive

    Hello there,

    I apologize if this has been asked before. I did some searching on these forums and thought I had found the start of a solution but it’s not working and I don’t know if it’s because I have the code wrong or the whole approach wrong. My situation:

    I have a shop page that I’m building for a customer that has multiple product categories and subcategories. On the woocommerce archive page, I have a sidebar with a search and filter form that allows product search, category multi-select, price range, and sort order. It works excellent for ALL products, but I’m afraid the end user will get confused when they navigate to a product category via the main navigation, then try to use the search and filter form to search / filter JUST that category. Anytime you use the form it searches all products, which redirects to the main shop page.

    My searches led me to the Edit Query Args function, which suggests that I could alter the function to have a conditional statement for whether or not the page was a product category, then filter the arguments for just that category. Here’s my code:

    
    function ag_filter_category_search( $query_args, $sfid ) {
    
      if ( is_product_category() ) {
       $category = get_queried_object();
       $cat_id = $category->term_id;
    	
       //if search form ID = 225, the do something with this query
       if($sfid==4852)
         {
           $query_args = array (
             'product_cat' => $cat_id,
           );
         }
    	
          return $query_args;
      }
    }
    add_filter( 'sf_edit_query_args', 'ag_filter_category_search', 10, 2 );
    

    This is currently not doing anything that I can see, but it might be because the code itself is wrong. What I need to accomplish if possible (two options?):

    1. When the user clicks to a category or subcategory page, have that automatically get added to the S&F form under the multiselect categories field. This way any further searches or filters will honor the current category (and the results will STAY on that category page rather than redirecting to the base “shop” page).

    2. When the user is on a category page, change the S&F form to change the search query arguments to restrict results only to that category (and not leave the category page). Could this be something like filtering the URL that comes out of the search form? For example:
    http://mysite.com/shop/?_sft_search_term=blahblah could be changed to:
    http://mysite.com/product-category/category-name/?_sft_search_term=blahblah

    The reason I’d not like to leave the category page is there is a custom display for each category that includes a category feature image and description.

    #39398

    Ross
    Keymaster

    Hey Paul

    You don’t need to do this via query args – you can do all this via the posts tab.

    If it must be dynamic and via the query args, then its the same as when using the standard new WP_Query($args)

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

    So, to modify orderby you need this part of the WP docs:

    https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    Updating the S&F filter example with the example from the WP site on order:

    $args = array(
    	'orderby' => 'title',
    	'order'   => 'DESC'
    );
    $query = new WP_Query( $args );

    Will update the filter like:

    function filter_function_name( $query_args, $sfid ) {
    	
    	//if search form ID = 225, the do something with this query
    	if($sfid==225)
    	{
    		//modify $query_args here before returning it
    		$query_args['orderby'] = 'title';
    		$query_args['order'] = 'DESC';
    		
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 10, 2 );

    Thanks

    #38917

    In reply to: Search Box


    Ross
    Keymaster

    Hi there

    If you must edit the query best not to hack the plugin directly 😉

    Instead, you can use the filter sf_edit_query_args – which allows you to change any of the parameters that are passed to the query:

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

    If you lower the priority to say 20:

    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );

    This will happen after S&F has setup the query.

    This means you should be able to access the object like:

    function filter_function_name( $query_args, $sfid ) {
    	
    	//if search form ID = 225, the do something with this query
    	if($sfid==225)
    	{
    		//modify $query_args here before returning it
                    
    		//here you can limit the length of 's' which is the search term
    		$query_args['s'] = "overwritten search term";
    	}
    	
    	return $query_args;
    }

    Thanks

    #37242

    In reply to: Filter Query Args


    Anonymous
    Inactive

    Ross,
    I just tested this and found that if I display results using the search.php then the filter can’t use the get_queried_object()->name; , but if I display results using the shortcode method within the same taxonomy template, the get_queried_object()->name; is usable for the query filter.

    Here’s what I currently have:

    function member_query_args( $query_args, $sfid ) {
    	
    	$current_member_category = get_queried_object()->name;
    	
      if($sfid==3906) {
    	 $query_args = array(
        'member-category' => $current_member_category
    	);
      }
      return $query_args;
    }
    
    add_filter( 'sf_edit_query_args', 'member_query_args', 10, 2 );
    #37151

    In reply to: Filter Query Args


    Anonymous
    Inactive

    OK,
    I’m able to get this to filter:

    function member_query_args( $query_args, $sfid ) {

    if($sfid==3906) {
    $query_args = array(
    ‘member-category’ => ‘hotels-motels’
    );
    }
    return $query_args;
    }

    How can I go about dynamically placing the value of member-category ?

    add_filter( ‘sf_edit_query_args’, ‘member_query_args’, 10, 2 );

    #37123

    Anonymous
    Inactive

    Hello Support,
    I’m trying to filter search results based on a taxonomy item. I’m trying to this code that I found in the forum, but I can’t get it to work even just using $query_args[‘order’] = ‘desc’;

    What am I doing wrong here? Below is the the code I’ve been trying.

    function sf_filter_query_args( $query_args, $sfid ) {

    if($sfid==3906) {
    $query_args[‘member-category’] = get_queried_object()->name;
    //$query_args[‘order’] = ‘desc’;
    }
    return $query_args;
    }

    add_filter( ‘sf_edit_query_args’, ‘sf_filter_query_args’, 10, 2 );

    #28855

    Ross
    Keymaster

    Hi Sergey

    Its just the same as the $args object you pass to a new WP_Query – so this link should provide you with the necessary options:

    https://codex.wordpress.org/Class_Reference/WP_Query

    To take their category example and apply it to the S&F filter:
    https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

    =

    <?php
    function sf_filter_query_args( $query_args, $sfid ) {
      
      //if search form ID = 225, the do something with this query
      if($sfid==225)
      {
      	//modify $query_args here before returning it
            //only show results within categories with these IDs
      	$query_args['cat'] = '2,6,17,38';
      }
      return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'sf_filter_query_args', 10, 2 );
    ?>

    Thanks

    #27945

    In reply to: Default search


    Ross
    Keymaster

    Hey Keith

    Unfortunately this is not yet an option within S&F – but there are a couple of hacky workarounds.

    1) Enable “Bookmarkable URLs” – then go to you search form, click “most popular” and you will notice the URL change. Use this URL for linking to your search page.

    2) Use the filter sf_edit_query_args (this is a lot more complex) – http://www.designsandcode.com/documentation/search-filter-pro/2-0-upgrade-notes/#No_more_pre_get_posts

    Then you would have to detect whether a search has been performed (ie any options in the form selected) – if not you would use the above filter to by tag 1 – and then using JS you would also have to set the filter – its pretty messy but I guess possible.

    Thanks

    #26679

    Anonymous
    Inactive

    Sorry, I can’t go by this way, because I can’t pass category or tag into sf_edit_query_args 🙁

    Category isn’t fixed, it get from post with vitrine shortcode. So different vitrines have different categories, but shortcodes in the posts can be same!

Viewing 10 results – 191 through 200 (of 203 total)
Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.