Forums Forums Search & Filter Pro Unindexing/removing/excluding certain posts programitcally via php hooks

Viewing 3 posts - 1 through 3 (of 3 total)
  • Anonymous
    #136459

    Hello

    I have the following Post Type/Taxonomy setup:

    Post type: “Grants”
    – Custom Taxonomy: “Person”
    – Custom Taxonomy: “Program”
    — Taxonomy term for this example “Youth Service Improvement Grants”
    Post Type: “People”

    The “Person” taxonomy creates a relationship between the custom post type “People”– the “Person” terms correspond one to one (they share the same slug) with a “People” post.

    What I want to do is *exclude* certain People posts from displaying in the search filter pro results page. The logic for exclusion is:

    If a Person tax term is only used on Grants with the “Program” taxonomy term called “Youth Service Improvement Grants” then find the corresponding “People” post and **exclude** that post from the search filter pro results page.

    I’ve tried doing this via the WordPress pre_get_posts hook, but it is not working. After adding this code I try rebuilding the index cache via the Search Filter admin panel and refreshing the front-end.

    How can I exclude certain posts from being indexed?

    Pre get posts:

    
    	add_action('pre_get_posts', 'exclude_people', 10);
    	function exclude_people($query){
    
    	    if(!is_admin() && $query->is_main_query() && $query->is_search) :
    			$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->set('exact', true);
    	        $query->set('post__not_in', $posts_to_exclude);
    	    endif;
    	}
    
    Anonymous
    #136461

    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 );
    
    Trevor
    #136741

    Cool. Thanks for sharing.

Viewing 3 posts - 1 through 3 (of 3 total)