Forums Forums Search Search Results for 'sf_edit_query_args'

Viewing 10 results - 101 through 110 (of 175 total)
  • Author
    Search Results
  • #139831

    Trevor
    Participant

    It is possible you might achieve this by using the Edit Query Arguments filter, but I do not think it would be easy.

    Other users have posted examples of their own usage in the forums, so it might be worth searching for the filter name:

    sf_edit_query_args


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


    Anonymous
    Inactive

    This solved my problem:

    https://gist.github.com/rmorse/074db89682afa8501b15

    
    	function sf_filter_query_args( $query_args, $sfid ) {
    	  
    			$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( 'sf_edit_query_args', 'sf_filter_query_args', 10, 2 );
    
    #135753

    Trevor
    Participant

    Not in the Post Meta tab, you you should be able to make something that would work with your own code using this filter:

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

    If you search for sf_edit_query_args in the forum, you will also find some posts showing how other users have used the filter, I think.

    #135160

    Trevor
    Participant

    I am not sure, but it may be possible to use this filter:

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

    If you search for sf_edit_query_args you will find some recent posts manipulating author in the query, I think.

    #135093

    Anonymous
    Inactive

    Hi Trevor,

    thanks for pointing me to Edit Query Arguments Filter, must have missed that!

    Here is how I set things up:
    – a front end form for submitting private custom posts (cpt supports author)
    – search & filter restricted to
    – post type: my custom post type
    – post status: private

    I expected search & filter to take into account that limitation to private actually limits search results to current users posts, as a regular wordpress query would do. But maybe my setup is wrong.

    If s&f doesn’t work this way, consider this a feature request. Because otherwise it renders wordpress post status private useless.

    Next, I tried your approach of editing Arguments Filter, but it gives me no results. Probably wrong syntax?

    
    function filter_user_posts( $query_args, $sfid ) {
      $user_id = get_current_user_id();
    	
      //if search form ID = 109, do something with this query
      if($sfid==109)
      {
        //modify $query_args here before returning it
        $query_args = array(
          'author' => $user_id
        );
      }	
      return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_user_posts', 20, 2 );
    

    Speaking of security issues: I will further investigate the URL-snooping issue. But I also have a content restrictions on single post view in other places, so posts can’t be viewed by non-authors anyway.

    Cheers,

    Georg

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

Viewing 10 results - 101 through 110 (of 175 total)