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

Viewing 2 posts - 1 through 2 (of 2 total)
  • 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 2 posts - 1 through 2 (of 2 total)