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 - 41 through 50 (of 205 total)
  • Author
    Search Results
  • #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

    #242002

    Ross
    Keymaster

    Hi Keith

    The filter is sf_apply_filter_sort_post__in

    $post__in = apply_filters('sf_apply_filter_sort_post__in', $this->query_args['post__in'], $this->query_args, $this->sfid);

    Which takes an array of post IDs (the results S&F has found) and must return those posts.

    You’ll then have to disable sorting and sort by post__in in the query (actually, you might be able to leave this set as “default” ordreing in your search form options), you can use the sf_edit_query_args filter to do this.

    If you’re going to follow the link above exactly, the JOIN option might work for you anyway, but there’s two potential approaches for you to try.

    Thanks

    #239148

    RS van de Griendt
    Participant

    How would I go about adding the To-date into the query? This is the query args that I get when I add the sf_edit_query_args filter (even with dates put in):

    Array
    (
        [paged] => 1
        [search_filter_id] => 144
        [search_filter_override] => 
        [posts_per_page] => 10
        [post_status] => Array
            (
                [0] => publish
            )
        [meta_query] => Array
            (
            )
        [post_type] => cursus
        [post__in] => Array
            (
                [0] => 0
            )
    )

    I can’t see any of the form inputs here, the only thing that seems to change is when I put in a search word, the [s]-arg is added.


    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

    #237281

    Erez Cohen
    Participant

    Hi,

    I created a form that includes post meta fields. I would like it to also include posts that do not have these meta keys at all.
    It is especially a problem because one might use other fields to filter the results, but once the form is submitted the meta fields (range sliders in that case) are taken into account as well, and posts with no meta disappear.
    I tried to use the “sf_edit_query_args” filter, but it seems the meta_query is empty when it is applied…
    maybe I am using it wrong?

    Thank you very much!


    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 0https://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

    White
    Participant

    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
    Moderator

    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

    Ramon Tissler
    Participant

    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!

Viewing 10 results - 41 through 50 (of 205 total)