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 - 71 through 80 (of 205 total)
  • Author
    Search Results
  • #206995

    Matt
    Participant

    Would it be possible to intercept the query and alter the query using sf_edit_query_args.

    I would do something like this in that filter

                $start = '2016-01-01 00:00:00';
                $end = '2016-12-31 23:59:59';
    
                $query_args['meta_key'] = '_audiotheme_gig_datetime';
                $query_args['meta_query'] = array(
                        'key'     => '_audiotheme_gig_datetime',
                        'value'   => array( $start, $end ),
                        'compare' => 'BETWEEN',
                );

    Ideally I just need to alter the query prior with my own arguments around a specific meta field.

    #206964

    dpolonsky
    Participant
    This reply has been marked as private.
    #206932

    Trevor
    Moderator

    The sf_edit_query_args hook will ignore any post__in (did you use 2 underscores in yours?) argument you pass, but sorting should work.

    Can you post up the actual code you are using (inside code back ticks)?


    Ross
    Keymaster
    This reply has been marked as private.
    #204275

    Ross
    Keymaster

    Ok, so what we did was simply update form field “defaults” but we didn’t update the query as well.

    So what we need to do is, under the same conditions, restrict the query to that taxonomy term:

    function filter_query_categories( $query_args, $sfid ) {
    	
    	//if search form ID = 252649, the do something with this query
    	if($sfid==252649)
    	{
    		//if the marketplace taxonomy is not already in use / selected
    		if(!isset($_GET['_sft_marketplace'])){
    			
    			//modify $query_args here before returning it
    			$query_args['tax_query'] = array(
    				'relation' => 'AND',
    				array(
    					'taxonomy' => 'marketplace',
    					'field'    => 'slug',
    					'terms'    => array("best-wordpress-app-themes") //restrict it
    				)
    			);
    		}
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_query_categories', 20, 2 );

    That should just about do it.

    Let me know how you get on.

    Thanks

    #200673

    Trevor
    Moderator

    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
    Moderator

    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
    Moderator

    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

    Stijn De Witte
    Participant

    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/

Viewing 10 results - 71 through 80 (of 205 total)