Forums Forums Search Search Results for 'sf_edit_query_args'

Viewing 10 results - 91 through 100 (of 175 total)
  • Author
    Search Results
  • #171164

    In reply to: Sort by pages first


    Trevor
    Participant

    It is not possible to do this with our normal settings, but you might be able to do this using our Edit Query Arguments filter:

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

    You can search for how other users have used this filter (for various uses) using this search:

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

    #169717

    Ross
    Keymaster

    Hi there, Trevor has asked me to take a look at this.

    In regards to post meta relation, do you mean this is not working when using our filter sf_edit_query_args?

    We actually pass those arguments, to a new WP_Queryhttps://codex.wordpress.org/Class_Reference/WP_Query

    Why not try creating your own custom new WP_Query (to make sure your date meta query works as expected first), and then try to apply that to the S&F query using our filter?

    Thanks

    #169305

    Anonymous
    Inactive

    I have tried, it’s not working. Maybe there is a mistake? I although don’t see any possibility in admin-panel to add relation ‘or’ to meta_query.

    function add_query_args($query_args, $sfid) {
            if ($sfid == 218) {
                date_default_timezone_set('Europe/Kiev');
                $now = date('Y-m-d H:i:s');
                $query_args = array(
                    'meta_query' => array(
                        'relation' => 'OR',
                        array(
                            'key'       => 'event-date-time-start',
                            'compare'   => '>=',
                            'value'     => $now,
                            'type'      => 'DATETIME',
                        ),
                        array(
                            'key'       => 'event-date-time-finish',
                            'compare'   => '>=',
                            'value'     => $now,
                            'type'      => 'DATETIME',
                        ),
                    ),
                );
            }
            
            return $query_args;
        }
    
        add_filter('sf_edit_query_args', 'add_query_args', 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;
    }
    #155099

    Ross
    Keymaster

    Hi Debbie

    There are a couple of issues with the code.

    You are setting up $args variable but returning $query_args so your modifications are not being passed through.

    Also using $args = replaces all the data in the existing query, we don’t want to replace, but add your modification.

    Without checking if your date query actually works, my suggestion would be to modify the code like:

    //Modify query args for filter UI on Productions archive template
    
    function filter_function_name( $query_args, $sfid ) {
      
    	//if search form ID = 6128, the do something with this query
    	if($sfid==6128)
    	{
    		//Today's date
    		$today = date('Y-m-d H:i:s'); 
    		$meta_query = array(
    			'key' => 'event_date',
    			'value' => $today,
    			'compare' => '<=',
    		);
    		$query_args['meta_query'] = array($meta_query);
    	}
      
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );

    Thanks

    #155080

    Anonymous
    Inactive

    Ok, I figured out a way to make it work, but I’m still running into one more issue.

    I reset the search form to use display results as a post type archive.

    I then created a separate page template that just displays upcoming productions, and am using the archives-productions.php template to display past productions, instead of the other way around.

    The filter UI is showing now and the filter works. However, I only want to display past productions, and then filter through those using the plugin.

    I tried using the edit query arguments filter, but I can’t seem to get it to work.

    Here’s what I added to my functions.php file:

    //Modify query args for filter UI on Productions archive template
    
    function filter_function_name( $query_args, $sfid ) {
      
      //if search form ID = 6128, the do something with this query
      if($sfid==6128)
      {
                //Today's date
                $today = date('Y-m-d H:i:s'); 
    
        $args = array(
                'meta_query' => array(
            array(
                'key' => 'event_date',
                'value' => $today,
                'compare' => '<=',
            ),
            )
    
                  ); 
      }
      
      return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );

    Anonymous
    Inactive

    Hi, why “sf_edit_query_args” filter doesn’t work in ajax mode results ?

    #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 - 91 through 100 (of 175 total)