Search Results for 'function sf_edit_query_args'

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

Viewing 10 results – 31 through 40 (of 141 total)
  • Author
    Search Results
  • #242232

    Anonymous
    Inactive

    Hi there,
    we would like to add a reltion OR about two field.
    We can’t set it in admin panel so we try to integrate it by php.
    In function.php we add this:

    function filter_results( $query_args, $sfid ) {
    	if($sfid==3981) {
    	$query_args = array(
    	    "meta_query" => array(
                        'relation' => 'OR',
                        array(
                            'key' => 'immobile_disponibile',
                            'compare' => 'NOT EXISTS',
                        ),
                        array(
                            'key' => 'immobile_disponibile',
                            'value' => '1',
                            'compare' => 'NOT LIKE',
                        ),
                    ),
    		"meta_key" => "immobile_disponibile",
    		'orderby' => 'menu_order',
    		"order" => "ASC",
                 );	
    	}
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_results', 20, 2 );

    But… not work.

    #242101

    Ross
    Keymaster

    No not really – we don’t have much in terms of devs accessing our functions to do DB queries etc our way – so so the sf_edit_query_args is usually the way to go.

    If your meta query requires a meta key to be an exact value, I can provide some code to set this in the query which should be much faster – its a bit clunky (we hope to improve this) but it taps in to our tables for a performance boost.

    Let me know what you’re trying to do (share the snippet of code if you like)

    Thanks


    Ross
    Keymaster

    Hi Sigurd

    After scratching my head I managed to figure out the issue.

    We perform our search outside of the regular WP_Query, so if you choose a category in our search form, WP doesn’t actually “know” its restrciting to a specific category, because our search does that, so WP appends the stick post…

    I’ve managed to come up with some code, that add the WP category to the WP_Query (even though our plugin has already restricted the posts to this category), but now WP also knows that at a category restriction is happening, and excludes the sticky:

    add_filter("sf_edit_query_args", "add_wp_category_to_query", 100, 2);
    function add_wp_category_to_query($query_args, $sfid){
    	
    	//if($sfid == 1234){ //you can restrict this behaviour to a search form ID
    	
    	//get user category selection from the search form (ie, detect which categories have been selected)
    	global $searchandfilter;
    	$query_data = $searchandfilter->get($sfid)->current_query();
    	$query_data_array = $query_data->get_array();
    	if(isset($query_data_array['_sft_category'])){
    		$category_values = $query_data_array['_sft_category']['active_terms'];
    		$current_category_ids = array(); //this will contain all the IDs of the categories that have been selected in the search form
    		foreach($category_values as $category){
    			array_push($current_category_ids, $category['id']);
    		}
    		//restrict query to WP category
    		$query_args['category__in'] = $current_category_ids;
    	}
    	//}
    	
    	return $query_args;
    }

    As there may be some small overhead when implementing this code, you can uncomment the first if( line, and restrict this to a specific search form / query.

    I hope that makes sense & works!

    Best


    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!

    #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!

    #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

Viewing 10 results – 31 through 40 (of 141 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.