Forums Forums Search Search Results for 'sf_edit_query_args'

Viewing 10 results - 61 through 70 (of 175 total)
  • Author
    Search Results
  • #200673

    Trevor
    Participant

    I think it might be possible using the Edit Query Arguments filter to do this. I do not recall a user asking this and achieving it, so I am unsure there are any snippets you might use, but a working knowledge of how WordPress structures its arguments would be need to code this yourself. This search would likely yield any snippets posted in our forum:

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

    #200431

    Trevor
    Participant

    Ah, try this type of method then:

    function include_exclude_search_filter( $query_args, $sfid ) {
    	
    	//if search form ID = 135, the do something with this query
    	if($sfid==135)
    	{
    		//modify $query_args here before returning it
    		$query_args['tax_query'] = array(
    		
    			'relation' => 'AND',
    			array(
    				'taxonomy' => 'question_category',
    				'field'    => 'term_id',
    				'terms'    => array( 254 ),
    				'operator' => 'IN',
    			),
    			array(
    				'taxonomy' => 'question_category',
    				'field'    => 'term_id',
    				'terms'    => array( 253 ),
    				'operator' => 'NOT IN',
    			)
    		);
    	}
        elseif($sfid==1234)
        {
            more stuff
        }
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'include_exclude_search_filter', 20, 2 );
    #200325

    Ross
    Keymaster

    Hi Jeroen

    Thanks for your patience, I finally understood the use case!

    I had a little think about this, my first issue was I thought a WP_Query couldn’t do this, because how would it know which rule takes precedent..?

    Anyway, turns out it is possible, which mean you can use our filter (sf_edit_query_args) to do this programmatically. This worked for me locally (you’ll have to replace some variables):

    //include and exclude a tax / category
    function include_exclude_search_filter( $query_args, $sfid ) {
    	
    	//if search form ID = 135, the do something with this query
    	if($sfid==135)
    	{
    		//modify $query_args here before returning it
    		$query_args['tax_query'] = array(
    		
    			'relation' => 'AND',
    			array(
    				'taxonomy' => 'question_category',
    				'field'    => 'term_id',
    				'terms'    => array( 254 ),
    				'operator' => 'IN',
    			),
    			array(
    				'taxonomy' => 'question_category',
    				'field'    => 'term_id',
    				'terms'    => array( 253 ),
    				'operator' => 'NOT IN',
    			)
    		);
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'include_exclude_search_filter', 20, 2 );

    Make sure to add this to your functions.php

    Things that need to be replaced in the above code:
    1) at the top you need to replace the search form ID with your own, so replace 135 with 17289
    2) The taxonomy name I was using was question_category, replace this with the name of your taxonomy
    3) The part that has the IN condition, is the equivalent of include
    'terms' => array( 254 ),
    So change 254 for the ID you want to include.
    4) The next part is the exclude:
    'terms' => array( 253 ),
    So change 253 for the ID of the category you want to exclude.

    If you need to know the IDs of your categories/taxonomies, just go to the edit page and check the URL for the ID.

    And thats it! You should be good to go, let me know how you get on.

    Thanks

    #198939

    Trevor
    Participant

    With regard to post__in, I am sorry, but you cannot update (in the filter sf_edit_query_args) that variable, and if used in $args, it is overwritten by our argument, because (after that filter is run) S&F does all the calculations from the cache, and replaces the post__in variable completely.

    #198936

    Anonymous
    Inactive

    Hi,

    We have set-up a custom query with an ACF relationship field.

    This works without S&FP.

    When we add 'search_filter_id' => 4356, to the WP Query, suddenly all posts are shown and not only the ones we defined in our WP_Query. (when we comment it out, it works)

    Below the full code

    	
            // Get account linked to logged in user
    	$account = get_field( 'field_5c3a01aa0a32e', 'user_' . get_current_user_id() );
    	
    	// Get lassers from that account: returns array of ID's
    	$lassers = get_field( 'field_5c3a0bb483f86', $account[0]->ID );
    
    	// args Query
    	$args = array(
    		'post_type'      => 'lasser',
    		'posts_per_page' => -1,
    		'post_status'    => 'publish',
    		'order'          => 'ASC',
    		'search_filter_id' => 4356,
    		'post__in'       => $lassers,
    
    	);

    We also tried the filter sf_edit_query_args but that doesn’t work either.

    Any idea why the above isn’t working/

    #196685

    Trevor
    Participant

    If you are trying to use the sf_edit_query_args to set a default value (and I agree it looks like this is an ideal case use for this filter), it does not work on initial page load (from my previous testing). This will not be addressed (in a different way) until V3.

    #195017

    Anonymous
    Inactive

    I’m having problems filtering sf_edit_query_args with the post__in query arg. Here’s a quick prototype that I have:

    
    add_filter( 'sf_edit_query_args', function( $query_args, $sfid ) {
        if ( 20205 === $sfid ) {
            $query_args['post__in'] = array( 20696, 20694, 20612 );
        }
    
        return $query_args;
    }, 20, 2 );
    

    I double-checked that 20205 is the id of the search&filter form and that 20696, 20694, 20612 are actual custom post type ids. The form works as it should with the usual search&filter search field, custom post type filters, tag dropdowns, etc. But I’m not able to filter it programmatically with the provided post ids (however, it works correctly with a provided custom post type – $query_args['post_type'] = 'custom';). It simply returns no results. Is the post__in query arg not supported?

    Thanks!


    Anonymous
    Inactive

    Take a look at this page. http://powerschoolnew.staging.wpengine.com/resources/?post_types=whitepaper

    I have 7 sticky posts in this post type. You can find them in the HTML with class=”resource-block featured”. When I first load the URL above, only 3 of the sticky posts appear in the first set of results (I wrap them in a carousel using jQuery). As I scroll, the other sticky posts appear(they will be 50% wide. I then move them to the top with jQuery.

    Why aren’t my sticky posts ALL appearing first before all non-sticky posts? I tried deliberately setting ignore_sticky_posts to false using sf_edit_query_args but this made no difference.

    Thanks for any help you can offer.

    #191286

    In reply to: modify $query


    Anonymous
    Inactive

    Now I thought about the problem again and looked at the documentation. I found the following: “Edit Query Arguments”.

    Then I tried the following:

    function filter_function_name( $query_args, $sfid ) {
    
    	if($sfid==163) {
    
    		$query_args['tax_query'] = array(
    			'relation' => 'OR',
    			array(
    				'taxonomy' => 'region',
    				'field'    => 'slug',
    				'terms'    => array( 'Österreich' ),
    			),
    		);
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );

    Unfortunately this works exactly the other way round to what I want.
    Now only articles with the tag Austria are displayed.

    Is there any way to modify it to work the way I think it should?

    #189780

    Anonymous
    Inactive

    Depending on which values are chosen in the inputs for rw (22-26) and tc (1-3) the corresponding ws and fw-value is used in the sliders. That is, if I choose 22 as rw and 1 as tc then the values in those meta boxes for ws and fw are compared to the data chosen in the rangers (ws-22-1 and rw-22-1), so I basically would need a conditional range slider which is not available.

    The code above is used with “sf_edit_query_args”. (I have asdo tried using “sf_results_url” as mentioned in the first text, but that didn’t work since it only gets me the base-url.)

    But if this is not the way to use the filter, how is it suppose to work? I thought the filter “sf_edit_query_args” would alter the query for the search, what does it do then? Just in case that will give me some new idea on how to advance.

    Otherwise I’ll just have to figure out a way to update the search query ’cause this is an important feature of this filter.

Viewing 10 results - 61 through 70 (of 175 total)