Search Results for 'sf_edit_query_args'

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

Viewing 10 results – 41 through 50 (of 203 total)
  • Author
    Search Results

  • Ross
    Keymaster

    Hi Armando

    So I’ve been thinking a bit about this, and obvously at some stage in the future we want to improve the way all this works, from within the plugin.

    Let me just say, you can’t alter the query in the way you want using sf_edit_query_args – this is because we actually perform our search outside of the WP_Query, and insert the results… anyway, for now I see you have only two options going forward:

    1) Assign a default value (lets say 0) to all posts that don’t have a value, and make sure the range slider starts at 0. This is a lot easier to do than it may seem. You run a new WP_Query to find all posts that don’t have this value assigned (NOT EXISTS) – for example you can see here:
    https://wordpress.stackexchange.com/questions/80303/query-all-posts-where-a-meta-key-does-not-exist
    Then when you look through each results, all you do is use add_post_meta() to set it to 0 – https://developer.wordpress.org/reference/functions/update_post_meta/

    2) As you mentioned at the beginning, you wanted the results on page load, to match those after a search has been performed once the default range slider values are added. You can in fact do this, by going to the post meta tab in your search form, and adding two conditions in, to ensure all posts are above and below a specific values.

    I hope that explains things a bit and gives you some viable options to work with!

    Let me know if you have any follow up questions.

    Thanks

    #237164

    Ross
    Keymaster

    Hi again

    So I’ve had a chance to digest this ticket.

    In regards to your two issues which are both part of the same problem:

    The way a form submits / resets is to include the sliders at their min / max value. We will try to address this in v3 within the plugin, but for now, if you want to have the same set of results when you reset as when you land on the page you have 2 options:

    a) set a default value on all other posts – you can write a query to loop through all posts where there is no value for this post meta, and then auto add a value like 0

    b) restict the meta query to the slider min/max params (you would have to use fixed min/max limits), you can do that from the post meta tab in your search form – this way the results that are shown when landing on the page, are teh same as when the form is reset / submitted, and the range slider params are added to the URL.

    —

    In regards to the filter: sf_edit_query_args, this is run before S&F applies its workings… S&F does a different kind of query, outside of your WP_Query, which is then combined with the WP_Query, so you can never truly unset that value. Your best bet would be to simply do unset($_GET['filter_name']); (you can dump this in the bottom of your functions.php to test) before S&F grabs the value and applies its sorting.

    I hope that clears a few things up for you?

    Let me know if you have any further questions.

    Thanks

    #236976

    Anonymous
    Inactive

    I tried to unset the slider using sf_edit_query_args, but it doesn’t clear the search results. It still keeps the slider query!

    This is what I have tried:

    function filter_function_name( $query_args, $sfid ) {
    
        global $searchandfilter;
        $current_query = $searchandfilter->get(58376)->current_query()->get_array();
        
        
        unset( $current_query['_sfm_LENGTH_KEY'] );
        
        return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );

    But it doesn’t do anything! Posts are still being filtered with the slider query!

    #236926

    In reply to: Order by 3 x fields


    Trevor
    Participant

    You would need to return the form order settings to default and instead use this filter:

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

    Adding an orderby argument to the query. This search may yield some snippets:

    https://support.searchandfilter.com/forums/search/orderby+sf_edit_query_args/

    #230828

    Anonymous
    Inactive

    Hi there!

    I’m a bit out of ideas at the moment… I have a search form for a custom post type where I also need to search meta fields of a custom taxonomy within this post type. Depending on what is chosen in the form, the search values for the taxonomy changes, so I can not add the information directly to the post type.

    What I think about now as a possible solution is to alter the meta_query with filter “sf_edit_query_args”, use own non-dynamic select fields in the search form, make my queries in the function and just exclude posts with post__not_in.

    I can add the select fields with jQuery or maybe manipulate a templates? But these fields also need to update the ajax with their GET values so that I can use them with the filter…

    I hope you understand me and I can send you whatever you need. Maybe there is another way, too. I’m very open for any idea.

    Thank you very much!

    #227761

    Anonymous
    Inactive

    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

    Anonymous
    Inactive

    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!

    #221016

    Anonymous
    Inactive

    hi Ross, hi Trevor,

    you guys are really supportive and i read a lot of posts on the forum. i still can’t figure out what i am trying to do alone, so i thought i would reach out.

    1 – latest WP 5.2.3
    2 – S&F Pro
    3 – ACF Pro (latest)
    4 – custom theme from scratch

    on the news search page (which is the standard posts type of WP), i am trying to do this:

    1 – allow a category filter
    2 – a search box
    3 – allow a date range filter with the terms “past month, 6 month, past year, older than 1 year”

    1 and 2, no problem. works perfectly.

    for date range, since that’s not an existing feature, i am trying to trick it to working.

    i read about the action filters, and i read about the various hooks.

    i realize that’s not something that can be done with a regular date field on Post Date.
    so i need to find another way.

    I am able to add a 'date_query' in a sf_edit_query_args function. When I hard code it like that it works just fine. But I want to give the visitor the dropdown that allows that choice.

    I tried to inject a dropdown in the form but it was being ignored by the Ajax function.
    I tried to use a dummy ACF field, called “sf-date-range” which contains the options I want for that (-1 month, -6 month, etc.) and build the date_query from that. I am able to do so with the sf_edit_query_args function, like this:

    
    /* custom search and filter query update based on date range */
    function custom_date_query_for_sf( $query_args, $sfid ) {
    	
    	global $searchandfilter;
    	
    	$sf_current_query = $searchandfilter->get(2467)->current_query();
    	$customFieldsArray = $sf_current_query->get_array();
    	$customHumanDate = $customFieldsArray["_sfm_sf-date-range"]["active_terms"][0]["value"];
    	 
    	//if search form ID = 2467 // news search, the do something with this query
    	if($sfid == 2467)
    	{
    			
    		if ($customHumanDate && $customHumanDate != NULL) { 
    
    				$query_args['date_query'] =  array(array('after' => $customHumanDate,));
    
    			} 
    		
    		echo "<pre>";
    		print_r($query_args);
    		echo "</pre>";  
    	
    	}
    
    	return $query_args;
    
    }
    
    add_filter( 'sf_edit_query_args', 'custom_date_query_for_sf', 20, 2 );
    

    however, since that ACF field is not really being used, the search returns blank since those fields aren’t checked, ever. the query is built ok, and again when I hard code it without the use of that ACF field, works fine:

    this example, hard-coded, returns exactly what’s expected (posts older than 1 year and younger than 2 years) :

    `
    Array
    (
    [paged] => 1
    [search_filter_id] => 2467
    [search_filter_override] =>
    [posts_per_page] => 10
    [post_status] => Array
    (
    [0] => publish
    )

    [category__not_in] => Array
    (
    [0] => 42
    )

    [meta_query] => Array
    (
    )

    [post_type] => post
    [orderby] => Array
    (
    [date] => DESC
    )

    [date_query] => Array
    (
    [0] => Array
    (
    [before] => -1 year
    )

    [1] => Array
    (
    [after] => -2 year
    )

    )

    )
    ‘

    so I know my query works.

    when i use the ACF field to generate the dropdown and make it all Ajax and neat, i don’t get the right results, even though I get the right query built (I have to refresh the screen to see the query, but i see it and it’s good. by the way, can I make the print_r that’s got the query printout ajaxified so that it shows me the right values on form change?)…

    I think the issue is that the ACF field I am using as a dummy dropdown generator is being read and obviously no values are in there since it’s not really being used. I tried to “unset” or “ignore” the ACF field which I only use to make it ajax and friendly…

    do you have a suggestion on how to accomplish what I am trying to accomplish? I know that v3 will allow me to make my own fields (which is what I am trying to do really here but without that) – but that’s not something I can wait on at the moment.

    i can send you a link in private if you want to see what I am doing.

    much appreciated,

    thanks — 

    Kaz


    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

    Anonymous
    Inactive

    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 );
    
Viewing 10 results – 41 through 50 (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.