Forums Forums Search Search Results for 'orderby sf_edit_query_args'

Viewing 10 results - 11 through 20 (of 29 total)
  • Author
    Search Results
  • #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?

    #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.

    #138398

    Ross
    Keymaster

    Hi there

    Just taking a look at this.

    The reason we implemented the edit_query_args filter is exactly for this reason, the arguments get passed through to our count functions too, so these should be updated, and its why this filter should be used over the WP pre_get_posts

    So, this is odd.

    2 things I would suggest:

    1) We are only trying to edit the $query_args (more specifically the meta query), not replace all of them (by using = on the variable you replace anything in the array already), perhaps this is causing an issue. I would change your code above to:

    function filter_upcoming_events( $query_args, $sfid ) {
    	if($sfid==29642)
    	{
    		$query_args["meta_query"] = array(
    					"key" => "event_date_time",
    					"value" => time() - (60 * 60 * 2),
    					"compare" => ">="
    				),
    			"meta_key" => "event_date_time",
    			"order" => "ASC",
    			"orderby" => "meta_value",
    		);	
    	}
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_upcoming_events', 20, 2 );

    I would also make sure, that any Post Meta settings, defined in the settings form, are removed / disabled, as well as the order results option in posts tab in your search form (this is all just to be safe and ensure no conflicts).

    2) You could have a pre_get_posts somewhere else in your setup, causing this unusual behaviour, but if using the post meta settings in the form works fine (with the counts) then this may not be the issue. Lets see about option 1 first.

    Let me know how you get on.

    Thanks

    #136903

    Anonymous
    Inactive

    Thanks for that, I’ve got it working now but the count on the filters are wrong. The list of filters is now including all posts rather than the ones that match the new query?

    This wasn’t a problem when I used the Post Meta conditions through the plugin UI

    This is the function I have up to now…

    function filter_upcoming_events( $query_args, $sfid ) {
    	if($sfid==29642)
    	{
    		$query_args = array(
    			"meta_query" => array(
    					"key" => "event_date_time",
    					"value" => time() - (60 * 60 * 2),
    					"compare" => ">="
    				),
    			"meta_key" => "event_date_time",
    			"order" => "ASC",
    			"orderby" => "meta_value",
    		);	
    	}
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_upcoming_events', 20, 2 );

    Page is at http://www.our-pub.co.uk/wp

    As you can see, the England games under the Football filters have already been played so the England filter should be hidden as it’s empty. ticking the England box and submitting the search returns no results.

    Thanks in advance.

    #134891

    Anonymous
    Inactive

    Hey, guys!

    We’re trying to display posts differently for logged in users and anonymous users (i.e. some posts are available only for logged in users). The problem is that when connecting to the ‘sf_edit_query_args_after_custom_filter’ hook the cache query is affected and the results are incomplete; the ‘sf_edit_query_args’ hook the cache works correctly but the listing displays wrong counter.
    My question is: is there any hook that can alter only the results displayed so that the cache query is not affected?
    Bellow is the code needed to run on the results found:

    function _query_filter($query_args, $sfid) {
        $post_type = get_query_var( 'post_type' );
        $affected_post_types = array(
            'module',
            'learning-object',
            'success-story'
        );
    
        if ( !is_post_type_archive( $affected_post_types ) || is_admin() ) {
            return $query_args;
        }
    
        // echo '<pre>'; print_r( get_query_var( 'post_type' ) ); echo '</pre>'; exit;
    
        $meta_query = array(
            'relation'      => 'AND'
        );
        $posts_per_page = '12';
        $post_status = array(
            'publish'
        );
    
        if ( is_user_logged_in() ) {
            
            if ( current_user_can('partner') || current_user_can('manage_options') || ( current_user_can( 'member' ) && preg_match( '/^(\/step\/)/i', $_SERVER['REQUEST_URI'] ) ) ) {
                $post_status[] = 'draft';
                $post_status[] = 'private';
                $post_status[] = 'internal';
            }
        }
    
        
        $query_args['orderby'] = 'date';
        $query_args['order'] = 'DESC';
    
        if ( $post_type != 'learning-object' ) {
            $posts_per_page = 6;
        }
    
        if ( !isset( $_GET['_sfm_language'] ) ) {
            $selected_language = '';
            if ( !defined( 'ICL_LANGUAGE_CODE' ) ) {
                define( 'ICL_LANGUAGE_CODE', 'en' );
            }
            
            switch ( ICL_LANGUAGE_CODE ) {
                case 'fi':
                    $selected_language = 'Finnish';
                    break;
                case 'de':
                    $selected_language = 'German';
                    break;
                default:
                    $selected_language = 'English';
                    break;
            }
    
            // $meta_query[] = array(
            //     'relation'		=> 'OR',
            //     array(
            //         'key'		=> 'language',
            //         'compare'	=> 'NOT EXISTS'
            //     ),
            //     array(
            //         'key'		=> 'language',
            //         'value'		=> $selected_language
            //     )
            // );
        }
    
        if ( !is_user_logged_in() ) {
    		$meta_query[] = array(
    			'relation'		=> 'OR',
    			array(
    				'key'		=> 'is_public',
    				'compare'	=> 'NOT EXISTS'
    			),
    			array(
    				'key'		=> 'is_public',
    				'value'		=> 'yes'
    			)
    		);
    	}
        
        $query_args['post_status'] = $post_status;
        $query_args['meta_query'] = $meta_query;
        $query_args['posts_per_page'] = $posts_per_page;
    
        // echo '<pre>'; print_r( $query_args ); echo '</pre>';
    
        return $query_args;
    }
    // add_filter( 'sf_edit_query_args', '_query_filter', 10, 2 );
    add_filter( 'sf_edit_query_args_after_custom_filter', '_query_filter', 10, 2 );

    Also, how can we update the counters to reflect only the displayed results?

    #99219

    Anonymous
    Inactive

    Hi Trevor,
    some of my WooCommerce products have a meta key “_tribe_event_starttime” and with your plugin I have setup an AJAX filter ordering these products by that meta key.
    Everything was fine for weeks but today I realized that the order has fallen back to the WC default (creation) “date”. You can see the result list by clicking on one of the big images on the top.

    https://obsthof-am-steinberg.de/produkt-kategorie/event-feste-kinder-musik/

    As you can see the AJAX requests has the GET parameter orderby=date and I tried to alter this order by hooking into your filter which I found here on your website:

    // alter query for search & filter results
    add_filter( ‘sf_edit_query_args’, ‘sf_orderby_eventdate’, 10, 2 );
    function sf_orderby_eventdate ($query_args, $sfid) {
    if ($sfid == 6047) {
    $query_args[“meta_key”] = ‘_tribe_event_starttime’;
    $query_args[“order_by”] = ‘meta_value’;
    $query_args[“order”] = ‘ASC’;
    }
    return $query_args;
    }

    Without success. I have installed WP Super Cache last week. Could this be the reason? Is there anything I missed or doing wrong?

    Thank you for your support!
    Oliver

    Now I am

Viewing 10 results - 11 through 20 (of 29 total)