Search Results for 'function sf_edit_query_args'

Forums › Forums › Search › Search Results for 'function sf_edit_query_args'

Viewing 10 results – 81 through 90 (of 141 total)
  • Author
    Search Results
  • #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 );
    #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.


    Anonymous
    Inactive

    I solved this shortly after asking the question.

    It was solved by placing the add_filter() calls to the posts_where and posts_join *AFTER* the “return $query_args;” line:

    
    		// REMOVE YOUTH SERVICE IMPROVEMENT GRANT PEOPLE
    		function sf_filter_query_args( $query_args, $sfid ) {
    			if(!is_library()) {
    				return false;
    			}
    			
    			$posts_to_exclude = array();
    			
    			$args = array(
    				'posts_per_page' => -1,
    				'post_type' => 'grants',
    				'tax_query' => array(
    				    array(
    				      'taxonomy' => 'program',
    				      'field' => 'slug',
    				      'terms' => array('youth-service-improvement-grants'),
    				    ),
    				),
    			);
    			
    			$myposts = get_posts( $args );
    			foreach ( $myposts as $post ) : setup_postdata( $post );
    				$tags = get_the_terms($post->ID, 'person');
    				
    				if(!empty($tags)) {
    					
    					foreach($tags as $tag) {
    						$tag_slug = $tag->slug;
    						$tag_terms = array($tag_slug);
    	
    						$args = array(
    							'posts_per_page' => -1,
    							'post_type' => 'grants',
    							'tax_query' => array(
    								'relation' => 'AND',
    							    array(
    							      'taxonomy' => 'person',
    							      'field' => 'slug',
    							      'terms' => $tag_terms
    							    ),
    		                        array(
    							        'taxonomy' => 'program',
    							        'field'    => 'slug',
    				      				'terms' => array('youth-service-improvement-grants'),
    				      				'operator' => 'NOT IN'
    		                        ),
    							)
    						);
    						
    						$my_person_posts = get_posts( $args );
    						
    						if(count($my_person_posts) < 1) {
    							
    							$myperson = get_page_by_path( $tag_slug, OBJECT, 'people' );
    							
    							if(!empty($myperson)) {
    								$posts_to_exclude[] = $myperson->ID;
    							}
    						}
    					}
    				}
    			endforeach; 
    			wp_reset_postdata();
    			
    	        $query_args['post__not_in'] = $posts_to_exclude;
    	        
    	  		return $query_args;
    	  		
    			add_filter('posts_where','atom_search_where');
    			add_filter('posts_join', 'atom_search_join');
    		}
    		add_filter( 'sf_edit_query_args', 'sf_filter_query_args', 10, 200);
    

    Anonymous
    Inactive

    I’m using the following code to match the search term to post slugs as well as match taxonomy term slugs.

    This works great, except it is interfering with how the Search query works in conjunction with the other filters.

    For example:

    Right now I have the following lines from the below code commented out (i.e. those lines are not being executed):

    
    	add_filter('posts_where','atom_search_where');
    	add_filter('posts_join', 'atom_search_join');
    

    So here:
    https://wtg.wpengine.com/the-library?_sft_format=blog-posts&_sf_s=evidence

    If those lines **were not commented out** the results would basically ignore the “_sft_format=blog-posts” part of the query string completely.

    So I’m missing the part in my code where the search is joined with the rest of the S&F query.

    Please help me identify what I can do to fix this code.

    Thank you!!!

    `
    function sf_filter_query_args( $query_args, $sfid ) {
    if(!is_library()) {
    return false;
    }

    add_filter(‘posts_where’,’atom_search_where’);
    add_filter(‘posts_join’, ‘atom_search_join’);

    }
    add_filter( ‘sf_edit_query_args’, ‘sf_filter_query_args’, 10, 2 );

    function atom_search_where($where){
    global $wpdb;
    if (is_sf_search() && isset($_GET[‘_sf_s’])) {

    $thesearch = $_GET[‘_sf_s’];
    $thesearch = trim(rawurldecode($thesearch));

    $where .= “OR (t.name LIKE ‘%” . $thesearch . “%’ AND {$wpdb->posts}.post_status = ‘publish’) OR (t.slug LIKE ‘%” . $thesearch . “%’ AND {$wpdb->posts}.post_status = ‘publish’)”;
    }
    return $where;

    remove_filter(‘posts_where’,’atom_search_where’);
    }

    function atom_search_join($join){
    global $wpdb;
    if (is_sf_search())
    $join .= “LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id”;
    return $join;

    remove_filter(‘posts_join’, ‘atom_search_join’);
    }

    #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

    #137453

    Anonymous
    Inactive

    Hi Trevor,

    a quick update, just in case someone is looking for a solution. The following filter works for me,
    only posts written by currently logged-in user will be displayed.

    function filter_function_name( $query_args, $sfid ) {
      //if search form ID = 100, then do something with this query
      if($sfid==100) {
        //modify $query_args here before returning it
        $query_args = array(
          'post_type' => 'my_custom_post_type',
          'author' => $user->ID
        );
      }
      return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );

    Thanks,

    Georg

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

Viewing 10 results – 81 through 90 (of 141 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.