Forums Forums Search Search Results for 'sf_edit_query_args'

Viewing 10 results - 31 through 40 (of 175 total)
  • Author
    Search Results
  • #239148

    Anonymous
    Inactive

    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

    Anonymous
    Inactive

    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

    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!

Viewing 10 results - 31 through 40 (of 175 total)