Search Results for 'orderby sf_edit_query_args'

Forums › Forums › Search › Search Results for 'orderby sf_edit_query_args'

Viewing 10 results – 11 through 20 (of 34 total)
  • Author
    Search Results
  • #221016

    Anonymous
    Inactive

    hi Ross, hi Trevor,

    you guys are really supportive and i read a lot of posts on the forum. i still can’t figure out what i am trying to do alone, so i thought i would reach out.

    1 – latest WP 5.2.3
    2 – S&F Pro
    3 – ACF Pro (latest)
    4 – custom theme from scratch

    on the news search page (which is the standard posts type of WP), i am trying to do this:

    1 – allow a category filter
    2 – a search box
    3 – allow a date range filter with the terms “past month, 6 month, past year, older than 1 year”

    1 and 2, no problem. works perfectly.

    for date range, since that’s not an existing feature, i am trying to trick it to working.

    i read about the action filters, and i read about the various hooks.

    i realize that’s not something that can be done with a regular date field on Post Date.
    so i need to find another way.

    I am able to add a 'date_query' in a sf_edit_query_args function. When I hard code it like that it works just fine. But I want to give the visitor the dropdown that allows that choice.

    I tried to inject a dropdown in the form but it was being ignored by the Ajax function.
    I tried to use a dummy ACF field, called “sf-date-range” which contains the options I want for that (-1 month, -6 month, etc.) and build the date_query from that. I am able to do so with the sf_edit_query_args function, like this:

    
    /* custom search and filter query update based on date range */
    function custom_date_query_for_sf( $query_args, $sfid ) {
    	
    	global $searchandfilter;
    	
    	$sf_current_query = $searchandfilter->get(2467)->current_query();
    	$customFieldsArray = $sf_current_query->get_array();
    	$customHumanDate = $customFieldsArray["_sfm_sf-date-range"]["active_terms"][0]["value"];
    	 
    	//if search form ID = 2467 // news search, the do something with this query
    	if($sfid == 2467)
    	{
    			
    		if ($customHumanDate && $customHumanDate != NULL) { 
    
    				$query_args['date_query'] =  array(array('after' => $customHumanDate,));
    
    			} 
    		
    		echo "<pre>";
    		print_r($query_args);
    		echo "</pre>";  
    	
    	}
    
    	return $query_args;
    
    }
    
    add_filter( 'sf_edit_query_args', 'custom_date_query_for_sf', 20, 2 );
    

    however, since that ACF field is not really being used, the search returns blank since those fields aren’t checked, ever. the query is built ok, and again when I hard code it without the use of that ACF field, works fine:

    this example, hard-coded, returns exactly what’s expected (posts older than 1 year and younger than 2 years) :

    `
    Array
    (
    [paged] => 1
    [search_filter_id] => 2467
    [search_filter_override] =>
    [posts_per_page] => 10
    [post_status] => Array
    (
    [0] => publish
    )

    [category__not_in] => Array
    (
    [0] => 42
    )

    [meta_query] => Array
    (
    )

    [post_type] => post
    [orderby] => Array
    (
    [date] => DESC
    )

    [date_query] => Array
    (
    [0] => Array
    (
    [before] => -1 year
    )

    [1] => Array
    (
    [after] => -2 year
    )

    )

    )
    ‘

    so I know my query works.

    when i use the ACF field to generate the dropdown and make it all Ajax and neat, i don’t get the right results, even though I get the right query built (I have to refresh the screen to see the query, but i see it and it’s good. by the way, can I make the print_r that’s got the query printout ajaxified so that it shows me the right values on form change?)…

    I think the issue is that the ACF field I am using as a dummy dropdown generator is being read and obviously no values are in there since it’s not really being used. I tried to “unset” or “ignore” the ACF field which I only use to make it ajax and friendly…

    do you have a suggestion on how to accomplish what I am trying to accomplish? I know that v3 will allow me to make my own fields (which is what I am trying to do really here but without that) – but that’s not something I can wait on at the moment.

    i can send you a link in private if you want to see what I am doing.

    much appreciated,

    thanks — 

    Kaz


    Anonymous
    Inactive

    Hi there,

    I have search and filter form where I need to set a custom meta query based on the values held within a dynamic array variable based on the user who is logged in (which controls what they see).

    So manually I tried the following:

    Post Meta > post_product = 1134

    Which is similar to what I want, and of course that works BUT I need the value of that query to be a dynamic array of product ID’s instead (held within $arr_product_ids in my example) depending on the user logged in, ie as follows:

    
       'meta_query' => array(
            'key'     => 'post_product',
            'value'   => $arr_product_ids,
            'compare' => 'IN',
        ),
    

    With that in mind, I have tried: https://searchandfilter.com/documentation/search-results/custom/ and added the following to my functions.php file but sadly this has not worked:

    
    
    global $arr_product_ids;
    
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 12,
        'category_name' => 'news',
        'meta_query'     => array(
            'relation' => 'AND',
            'post_product' => array(
                'key'     => 'post_product',
                'value'   => $arr_product_ids,
                'compare' => 'IN',
            ),
            'post_urgent' => array(
                'key'  => 'post_urgent',
                'compare' => 'EXISTS',
            ),
            'post_priority' => array(
                'key'  => 'post_priority',
                'compare' => 'EXISTS',
            ),
        ),
        'orderby'  => array( 'post_priority' => 'DESC', 'post_urgent' => 'DESC', 'date' => 'DESC' ),
    );
    $args['search_filter_id'] = 2978;
    $query = new WP_Query($args);
    
    

    I then looked at https://searchandfilter.com/documentation/action-filter-reference/ and tried adding the following to functions.php, but sadly again no luck:

    
    
    function filter_function_name( $query_args, $sfid ) {    
        if($sfid==2978) {
            $query_args['post_product'] = $arr_product_ids;
        }    
        return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    
    

    Please could you ket me know where I’m going wrong with this as I think I’m going around in circles sadly, whereas you will know the issue immediately I’m sure :).

    Many thanks,
    TJ

    #207512

    Anonymous
    Inactive

    Ok,

    I added the next code to function.php

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

    But now if I use the filter it is only showing 2 downloads:
    https://www.dl-sounds.com/member/downloads/?sort_order=name+asc&_sfm_bpm=0+1000

    without the filter it is working:
    https://www.dl-sounds.com/member/downloads

    I must be doing something wrong?

    #206917

    Anonymous
    Inactive

    I have a WP search bar that when the user types in the keyword, it directs them to the search.php template. There, the results display and there is a Search & Filter Pro form above the results.

    The good news is that for the same keyword, both the WP search and S&F Pro return the same number of results. The bad new is, that the order isn’t matching. I’m not sure why yet.

    I’m using the sf_edit_query_args hook to alter the query arguments for S&F. I’ve made it so the query vars from S&F match to the WP search which I’m also altering using pre_get_posts.

    The only thing that does not match, is that S&F has the post__in arg with IDs stored whereas the default WP search has it as an empty array. Could this be why the order of the results from S&F pro differ from that of the WP search results? I tried setting it to an empty array on the hook, and set the priority to 15 but it doesn’t seem to overwrite it.

    If it’s helpful, I’ve set these query args both for WP search and S&F search:

    order = DESC
    post__not_in = some IDs I’ve passed
    post_in = empty array (only for S&F where I’m trying to force it, WP already does this)
    orderby = ‘meta_value date’
    post_status = ‘publish’
    meta_query – Relation: OR, checks two keys whether they EXISTS/NOT EXISTS

    It seems like the meta query that’s not kicking in. On the WP search, it takes the posts with the post meta I’m looking for that exists, and places them on top of the results with the date/desc as the secondary sorting. Then the remaining results follow also in date/desc order.

    The S&F Pro results don’t seem to be doing that correctly. Anything I can try or other args/query vars I can check?

    Thanks.

    #200503

    Anonymous
    Inactive

    Hey Trevor,

    Thanks for trying to help me this am. I have implemented the hook in functions php but it has brought on other issues now.

    Issue #1 –
    Current page is not recognized. So when you click page 2, it does bring load page 2 via ajax but the “current” class is not applied to the number 2 in pagination. So “1” is still treated as the current page from css standpoint thus to the user it looks like they are on page 1.

    Issue #2 –
    The goal is for the homepage to display shows today and in future. But for the user to be able to search for all shows past and present. This is how I successfully had it set up at first. When using the below function, yes the homepage only displays shows in the future when you initially load the page but it also makes it so you can not search shows in the past. Again, the goal is for home page to display today and future shows but for the user to be able to search both past and future.

    function include_exclude_search_filter( $query_args, $sfid ) {

    `//if search form ID = 1113, the do something with this query
    if($sfid==1113)
    {
    //modify $query_args here before returning it
    $query_args[‘meta_query’] = array(

    ‘relation’ => ‘AND’,

    array(
    ‘post_type’ => ‘show’,
    ‘posts_per_page’ => 12,
    ‘max_num_pages’ => 99999,
    ‘meta_key’ => ‘show_date’,
    ‘order’ => ‘ASC’,
    ‘orderby’ => ‘meta_value’,
    ),
    array(
    ‘key’ => ‘show_date’,
    ‘compare’ => ‘>=’,
    ‘value’ => $today,
    ),
    );
    }
    elseif($sfid==1234)
    {

    }

    return $query_args;
    }
    add_filter( ‘sf_edit_query_args’, ‘include_exclude_search_filter’, 20, 2 );`

    #188189

    Anonymous
    Inactive

    Hi Trevor,

    We are getting close 🙂

    It’s important that they are properly ranked. That’s why I used following hook to change the order:

    /* Change query Search and filter pro */
    function filter_function_name( $query_args, $sfid ) {
    	
    	if($sfid==1710)
    	{
    		//modify $query_args here before returning it
    		$query_args['meta_key'] = 'prioriteit_mandaat';
    		$query_args['orderby'] = 'meta_value_num';
    		$query_args['order'] = 'ASC';
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    
    #168826

    Ross
    Keymaster

    Hi Liam

    I had a look (and was surprised that you could add an orderby with random seed to the query object so easily)! You learn something new every day!

    Anyway, I tested the code you were using and it mostly seemed correct – but I had some trouble getting it working.

    I realised, in this scenario, using our filter sf_edit_query_args was the wrong filter to use, because it is called several times (once for the main query, and again, while generating our filters) – which probably meant your seed being continually reset, or reset 3 times in 1 page load.

    — As a side note, the whole purpose of this filter we use is so that any modification you do to the query, is also reflected in the filter counts…

    However, in this case, ordering the results will have no effect on the filter counts, so its not necessary and we can go ahead and use pre_get_posts.

    Using your code, and some I’ve found online, I think I’ve managed to successfully do this:

    
    session_start();
    add_filter( 'pre_get_posts', 'sf_sort_random_seed', 200, 2 );
    function sf_sort_random_seed( $query ) {
    
    	if( !is_admin() && $query->is_main_query() ) {
    			
    		//$query->set( 'posts_per_page', 1 ); // testing
    		
    		//grab the ID from the query_vars
    		$sfid = 0;
    		if(isset($query->query_vars['search_filter_id'])) {
    			$sfid = $query->query_vars['search_filter_id'];
    		}
    
    		if($sfid==27339||$sfid==27361) {
    
    			// Reset seed on load of initial archive page
    			$sf_paged = 0;
    			if(isset($_GET['sf_paged'])){
    				$sf_paged = $_GET['sf_paged'];
    			}
    
    			if( $sf_paged == 0 || $sf_paged == 1 ) {
    				if( isset( $_SESSION['seed'] ) ) {
    					unset( $_SESSION['seed'] );					
    				}
    			}
    
    			// Get seed from session variable if it exists
    			$seed = false;
    			if( isset( $_SESSION['seed'] ) ) {
    				$seed = $_SESSION['seed'];
    			}
    
    			// Set new seed if none exists
    			if ( ! $seed ) {
    				$seed = rand();
    				$_SESSION['seed'] = $seed;
    			}
    
    			// Inject the order into the query
    			$query->set( 'orderby', 'RAND(' . $seed . ')' );
    		
    		}
    	}
    	
    	return $query;
    }

    This seems to be working for me anyway! Just watchout when using sessions, I’ve heard many cases of them not working in WP, and needing to use transients instead.

    Let me know if it works!

    Thanks

    #168110

    Anonymous
    Inactive

    Hi Trevor,

    I have been doing exactly that and I think I have gotten pretty close to getting this solved but I have hit another snag. I think the only problem I have now is that I cant get the session variable to stick whilst using the filter you supplied. The filter might be running before the session data is loaded or the session data may not work inside of the filter. I might need to find another method of storing the rand seed variable into user cookies or something similar.

    I have modified the sf form and taken it off rand. I am now using the function to apply the random order, which means I can control the incoming arguments whereas I couldnt before. I am collecting the query args from the filter then assessing the paged data and at that point will create or reuse the random seed session variable. The only problem I have is the filter is not detecting the stored session data, I assume this is because the filter is running before the session data is loaded? that would explain why its being reset every time.

    I feel like is pretty close to working and would really appreciate if you could ask your developer to take a quick look at the function. This is what I have so far:

    session_start();
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    function filter_function_name( $query_args, $sfid ) {
    
    	if($sfid==27339||$sfid==27361) {
    
    	  	// Detect first page and reset seed
    		if( ! $query_args['paged'] || $query_args['paged'] == 0 || $query_args['paged'] == 1 ) {
    			?><p>first page<p><?php 
    			if( isset( $_SESSION['seed'] ) ) {
    				unset( $_SESSION['seed'] );
    				?><p>seed unset<p><?php 	
    			}
    		} else {
    			?><p>not first page<p><?php 
    		}
    		?><p>session seed value: <?php print_r($_SESSION['seed']);?></p><?php 
    		
    		// Get seed from session variable if it exists and store it
    		$seed = false;
    		if( isset( $_SESSION['seed'] ) ) {
    			$seed = $_SESSION['seed'];
    			?><p>seed session reused : <?php echo $seed ;?><p><?php 
    		}
    	
        	// Set new seed if none exists
        	if ( ! $seed ) {
          		$seed = rand();
          		$_SESSION['seed'] = $seed;
          		?><p>seed session created: <?php echo $seed;?><p><?php 
        	}
    
        	// Inject the order into the query
        	$query_args['orderby'] = 'RAND(' . $seed . ')';
    
    	}
    
    	return $query_args;
    }
    
    #167512

    Anonymous
    Inactive

    I had a few cracks but so far I haven’t been able to get it working. Here is what my current code looks like.

    session_start();
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    function filter_function_name( $query_args, $sfid ) {
    
    	if($sfid==27339||$sfid==27361) {
    	  	// Reset seed on load of initial archive page
    		if( ! get_query_var( 'sf_paged' ) || get_query_var( 'sf_paged' ) == 0 || get_query_var( 'sf_paged' ) == 1 ) {
    			if( isset( $_SESSION['seed'] ) ) {
    				unset( $_SESSION['seed'] );
    			}
    		}
    	
    		// Get seed from session variable if it exists
    		$seed = false;
    		if( isset( $_SESSION['seed'] ) ) {
    			$seed = $_SESSION['seed'];
    		}
    	
    	    	// Set new seed if none exists
    	    	if ( ! $seed ) {
    	      		$seed = rand();
    	      		$_SESSION['seed'] = $seed;
    	    	}
    	
    	    	// Update ORDER BY clause to use seed
    	    	$order_args = array(
    	    		'orderby' => 'RAND(' . $seed . ')'
    	    	);
        	$query_args = wp_parse_args( $query_args, $order_args );
    	}
    
    	return $query_args;
    }
    #139846

    Anonymous
    Inactive

    Thanks, Trevor. I tried adding the following to my functions.php but didn’t see the results I was looking for either.

    function filter_function_name( $query_args, $sfid ) {
      //if search form ID = 100, then do something with this query
      if($sfid==261) {
        //modify $query_args here before returning it
        $query_args = array(
          'post_type' => 'resource-item',
          'suppress_filters' => true,
          'orderby' => 'post_date',
          'order' => 'DESC',
          'tag__in' => array( 151, 348, 419 ),
          'posts_per_page' =>  10
        );
      }
      return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );

    I’ve been searching through the forum, and haven’t been able to find an example that includes the tags.

Viewing 10 results – 11 through 20 (of 34 total)
Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.