Forums Forums Search Search Results for 'function sf_edit_query_args'

Viewing 10 results - 111 through 120 (of 123 total)
  • Author
    Search Results
  • #67175

    Anonymous
    Inactive

    One more question.
    Is there way to add extra parameter to shortcode?
    To set default query.

    i.e.

    [searchandfilter id="1428" show="results" post="123"]
    
    function sf_filter_query_args( $query_args, $sfid ) {
      if($sfid==1428)
      {
            /* i'm not sure how to get $shortcode_param */
      	$query_args['my_post_id'] = $shortcode_param['post'];
      }
      return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'sf_filter_query_args', 10, 2 );
    #50954

    Anonymous
    Inactive

    Hi Trevor,
    I used the code bellow in the functions.php file. Shoudn’t I ?
    Thank you 🙂

    function filter_function_name( $query_args, $sfid ) {

    //if search form ID = 1272, the do something with this query
    if($sfid==1272)
    {
    //modify $query_args here before returning it
    print_r($query_args);
    echo “Yeahhhhhhhhhhhh”;
    }

    //return $query_args;
    }
    add_filter( ‘sf_edit_query_args’, ‘filter_function_name’, 10, 2 );

    #41753

    Ross
    Keymaster

    Hi Christian

    I have not yet had chance to implement this feature – as mentioned it only works with archives at the moment – I have just realised though if you are a dev and can do some coding this may already be possible using a filter:

    http://www.designsandcode.com/documentation/search-filter-pro/action-filter-reference/#Edit_Query_Arguments

    So in there you would manually check to see if the current page/post has a specific category:

    https://codex.wordpress.org/Function_Reference/has_category

    if(has_category( 'dogs', get_the_ID()))
    {
    
    }

    And if so, modify the S&F query to only look inside that category

    $query_args['category_name'] = "dogs"

    So putting it all together:

    function update_search_category( $query_args, $sfid ) {
    	
    	//if search form ID = 225, the do something with this query
    	if($sfid==225)
    	{
    		//here we check to see value is associated with the page
    		
    		if(has_category( 'dogs', get_the_ID()))
    		{
    			$query_args['category_name'] = "dogs"
    		}
    		else if(has_category( 'cats', get_the_ID()))
    		{
    			$query_args['category_name'] = "cats"
    		}
    		else if(has_category( 'snakes', get_the_ID()))
    		{
    			$query_args['category_name'] = "snakes"
    		}
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'update_search_category', 10, 2 );

    One big caveat I just realised, you may have to disable ajax – this is because an ajax request gets sent off somewhere else, and I think has_category will never give the correct result in an ajax request.

    Thanks

    #41634

    Ross
    Keymaster

    There shouldn’t be a custom query inside the filter itself, the filter is used to modify the arguments before they get passed to a query – the code should look like:

    function results_map_filter( $query_args, $sfid ) {
    	
    	//if search form ID = 1530, the do something with this query
    	if($sfid==1530)
    	{
    		//modify $query_args here before returning it
    		$query_args['posts_per_page'] = -1;
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'results_map_filter', 10, 20 );

    Thanks

    #41626

    Trevor
    Participant

    I have no idea if your code is roughly correct, but it is not semantically correct.

    It would look like this to be correct from a PHP point of view, I think:

    <?php
    function results_map_filter( $results_map_args, $sfid ) {
      if($sfid==1530) {
        $results_map = get_posts( $results_map_args );
        if (have_posts() ) {
          $results_map_args = array(
            'post_type' => 'posttype',
            'posts_per_page' => -1,
            'post_status' => 'publish',
            'cache_results' => true,
            'no_found_rows' => true
          );
        }
        return $results_map_args;
      }
    add_filter( 'sf_edit_query_args', 'results_map_filter', 10, 2 );
    ?>

    You would use this in your child theme functions.php file, which should already have the opening PHP tag <?php, so you wouldn’t need that from the code, and it is NOT normal to close the PHP tags ?> at the end of the functions.php file as this can cause a site to crash.

    I could well be wrong with the code though.

    #41625

    Anonymous
    Inactive

    Can you please explain more. I have this code now but the number of results don’t change

    <?php function results_map_filter( $results_map_args, $sfid ) {
    if($sfid==1530) {
    $results_map_args = array(
    ‘post_type’ => ‘posttype’,
    ‘posts_per_page’ => -1,
    ‘post_status’ => ‘publish’,
    ‘cache_results’ => true,
    ‘no_found_rows’ => true
    );
    }
    return $results_map_args;
    }
    add_filter( ‘sf_edit_query_args’, ‘results_map_filter’, 10, 2 );
    $results_map = get_posts( $results_map_args );
    if (have_posts() ) :
    ?>

    #41395

    Ross
    Keymaster

    Hey Matt

    This is pretty tricky actually.

    Since the introduction of the cache, most queries are sent off to the cache DB, and they return a number of IDs… which are placed inside the post__in field in a query.

    So, I’m actually scratching my head as to how we could do this.

    There is a filter for modifying the query:

    http://www.designsandcode.com/documentation/search-filter-pro/action-filter-reference/#Edit_Query_Arguments

    add_filter( 'sf_edit_query_args', 'filter_function_name', 10, 2 );

    Which should be used like:

    function filter_function_name( $query_args, $sfid ) {
    	
    	//if search form ID = 225, the do something with this query
    	if($sfid==225)
    	{
    		//modify $query_args here before returning it
    		$query_args['post__in'] = array(1,2,3,4);
    	}
    	
    	return $query_args;
    }

    Which S&F also uses for setting up the query – and will probbably overeride this withe the results from the S&F cache.

    Try initializing hook much later (so S&F has finished with the post__in var):

    add_filter( 'sf_edit_query_args', 'filter_function_name', 10, 200 );

    Thanks

    #39398

    Ross
    Keymaster

    Hey Paul

    You don’t need to do this via query args – you can do all this via the posts tab.

    If it must be dynamic and via the query args, then its the same as when using the standard new WP_Query($args)

    https://www.designsandcode.com/documentation/search-filter-pro/action-filter-reference/#Edit_Query_Arguments

    So, to modify orderby you need this part of the WP docs:

    https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    Updating the S&F filter example with the example from the WP site on order:

    $args = array(
    	'orderby' => 'title',
    	'order'   => 'DESC'
    );
    $query = new WP_Query( $args );

    Will update the filter like:

    function filter_function_name( $query_args, $sfid ) {
    	
    	//if search form ID = 225, the do something with this query
    	if($sfid==225)
    	{
    		//modify $query_args here before returning it
    		$query_args['orderby'] = 'title';
    		$query_args['order'] = 'DESC';
    		
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 10, 2 );

    Thanks

    #38917

    In reply to: Search Box


    Ross
    Keymaster

    Hi there

    If you must edit the query best not to hack the plugin directly 😉

    Instead, you can use the filter sf_edit_query_args – which allows you to change any of the parameters that are passed to the query:

    https://www.designsandcode.com/documentation/search-filter-pro/action-filter-reference/#Edit_Query_Arguments

    If you lower the priority to say 20:

    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );

    This will happen after S&F has setup the query.

    This means you should be able to access the object like:

    function filter_function_name( $query_args, $sfid ) {
    	
    	//if search form ID = 225, the do something with this query
    	if($sfid==225)
    	{
    		//modify $query_args here before returning it
                    
    		//here you can limit the length of 's' which is the search term
    		$query_args['s'] = "overwritten search term";
    	}
    	
    	return $query_args;
    }

    Thanks

    #37242

    In reply to: Filter Query Args


    Anonymous
    Inactive

    Ross,
    I just tested this and found that if I display results using the search.php then the filter can’t use the get_queried_object()->name; , but if I display results using the shortcode method within the same taxonomy template, the get_queried_object()->name; is usable for the query filter.

    Here’s what I currently have:

    function member_query_args( $query_args, $sfid ) {
    	
    	$current_member_category = get_queried_object()->name;
    	
      if($sfid==3906) {
    	 $query_args = array(
        'member-category' => $current_member_category
    	);
      }
      return $query_args;
    }
    
    add_filter( 'sf_edit_query_args', 'member_query_args', 10, 2 );
Viewing 10 results - 111 through 120 (of 123 total)