Support Forums

The forums are closed and will be removed when we launch our new site.

Looking for support? You can access the support system via your account.

Forums Forums Search Search Results for 'sf_edit_query_args'

Viewing 10 results - 51 through 60 (of 205 total)
  • Author
    Search Results
  • #227761

    Greg Smuk
    Participant

    Hi there,

    So I have a list of reference IDs (ACF Field for each post) in an array which I use in a meta_query to filter posts further after search and filter does its thing. This array contains post references in a specific order and the order I would like the posts to display.

    Here is the query I am using:

    ‘paged’ => int 1
    ‘search_filter_id’ => int 47
    ‘search_filter_override’ => boolean false
    ‘posts_per_page’ => int 10
    ‘post_status’ =>
    array (size=1)
    0 => string ‘publish’ (length=7)
    ‘meta_query’ =>
    array (size=1)
    0 =>
    array (size=3)
    ‘key’ => string ‘post_reference’ (length=13)
    ‘value’ => ARRAY OF POST REFS HERE
    ‘compare’ => string ‘IN’ (length=2)
    ‘post_type’ => string ‘custom_post’ (length=8)
    ‘orderby’ => string ‘meta_value’ (length=10)`

    Now obviously we can’t override post__in which would be ideal as I can pass an aray of post IDS rather than references, is there no way whatsoever we can do this?

    I tried to order by the meta_value which is the array of post refs, but it just orders them alphabetically rather than in the order they are in the given in the array.

    Can you think of a way I can work with SAFP to get this working correctly?

    Thanks

    #222024

    Alex Krijanovski
    Participant

    Hello!

    I’m having some trouble editing the search query. I’m using the plugin alongside Relevanssi and want results to be sorted by relevance as well as post_date or modified date. I tried adding this to the functions file

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

    But it doesn’t amend the search URL or results.

    Any help would be greatly appreciated!


    Ross
    Keymaster

    Hi Thomas

    So I’ve found a better way to do this (that can be search form specific).

    The code in full (so remove everything else we’ve added:

    
    function filter_input_object_radio($input_object, $sfid){
    
    	if(($input_object['name']!='_sf_post_type')||($input_object['type']!='radio'))
    	{
    		return $input_object;
    	}
    	
    	if(isset($input_object['options'])){
    		//remove the first option from the array
    		$removed_option = array_shift($input_object['options']);
    	}
    	
    	//now set the default value in the field
    	//make sure we only modify a field belonging to a specific Search Form, by ID
    	if($sfid==15485){ //replace 15485 with your search form ID
    		//make sure the field is not set
    		if(!isset($_GET['post_types'])){
    			//then set it to "post"
    			$input_object['defaults'] = array("post");
    		}
    	}
    	
    	return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'filter_input_object_radio', 10, 2);
    
    function set_default_post_type_query( $query_args, $sfid ) {
    	
    	//if search form ID = 225, the do something with this query
    	if($sfid==15485) //replace 15485 with your search form ID
    	{
    		//modify $query_args here before returning it
    		if(!isset($_GET['post_types'])){
    			$query_args['post_type'] = 'post';
    		}
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'set_default_post_type_query', 20, 2 );
    

    The only thing you have to replace is the search form ID of 15485 (there are 2 instances of this) – swap this out to match the ID of the search form on the homepage and it should work 🙂

    Best

    #217825

    Jordan
    Participant

    I am using the same filter to modify the main shop search too. This one works fine:

    
    function update_search_category( $query_args, $sfid ) {
    
      $query_args['tax_query'] = array(
        array(
          'taxonomy'      => 'product_cat',
          'terms'         => '20',
          'operator'      => 'IN',
        ),
      );
    
      return $query_args;
    
    }
    add_filter( 'sf_edit_query_args', __NAMESPACE__.'\\update_search_category', 20, 2 );
    
    #217813

    Trevor
    Moderator

    Why is this:

    add_filter( 'sf_edit_query_args', __NAMESPACE__.'\\user_liked_products_search_filter', 100, 2 );

    Not this:

    add_filter( 'sf_edit_query_args', 'user_liked_products_search_filter', 100, 2 );

    #217760

    Jordan
    Participant

    Trevor – Question regarding your notes on selecting the correct ACF meta key:
    https://support.searchandfilter.com/forums/topic/search-options-not-working-with-acf/#post-217575

    In our Post Meta form object Meta Key selection list, you need to use key #3

    Should we also be using the non underscore keys when we’re modifying the query via sf_edit_query_args too?

    #217706

    Jordan
    Participant

    Yes it is a function, I was just showing you only the parts which you needed to see, here is it all:

    
    /**
     * USER PROFILE - Modify search form args
     */
    function user_liked_products_search_filter( $query_args, $sfid ) {
    
      if ( $sfid == 26 ) {
        $query_args = array (
          'meta_query' => array(
            array(
              'key'       => '_likes',
              'value'     => 1,
            ),
          ),
        );
      }
    
      return $query_args;
    
    }
    add_filter( 'sf_edit_query_args', __NAMESPACE__.'\\user_liked_products_search_filter', 100, 2 );
    
    #215163

    Ross
    Keymaster

    Hi Kie

    What Trevor says is correct, you can use our filter sf_edit_query_args to restrict the results to certain categories or taxonomies, or exclude them, for example.

    Let us know how you get on.

    Thanks

    #215143

    Trevor
    Moderator

    I think you will need to use this filter (the code goes in to your child theme functions.php file):

    https://searchandfilter.com/documentation/action-filter-reference/#edit-query-arguments

    Like this:

    function exclude_password_protected_posts( $query_args, $sfid ) {
    	if($sfid==225)
    	{
    		$query_args['has_password'] = FALSE;
    	}
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'exclude_password_protected_posts', 20, 2 );

    where you need to change the ID number (in the example it is 225, so use the ID number of your form).

    #213642

    realityhouse
    Participant
    This reply has been marked as private.
Viewing 10 results - 51 through 60 (of 205 total)