Support

Account

Home Forums General Issues How to exclude posts from loop that are in a relationship Reply To: How to exclude posts from loop that are in a relationship

  • Hey! thank you very much, this sounds good!
    I tried it like this, at the moment the query does not run but I have an array with all ids of posts in relationship. What do you think, will this work solid?
    Why the query is not updating?

    the code is just a sketch at the moment:

    
    function ingruppe_pre_get_post( 	$the_query ) {
    
    	 wp_reset_postdata();
    
    	
    	$alle_posts_in_gruppen = array();
    
    if( ! is_admin() &&	$the_query->is_main_query() ) {
    	
    	$check_in_gruppe = get_posts(array(
     		'posts_per_page' => '-1',
     		'post_type' => array ('veranstaltungsgruppe'),
     		'meta_key' => 'veranstaltungen',
     
    
     				
     		));
    
    	foreach ($check_in_gruppe as $check_in_gruppe_i){
    		
    			$beziehung = get_field('veranstaltungen', $check_in_gruppe_i->ID);
    			
    			foreach	($beziehung as $beziehung_i) {
    				
    				$alle_beziehungen [] = $beziehung_i->ID;
    				
    			}	
    	}
    	
    	echo "<br>exclude this posts because they are in relationship: <br>";
    					print_r($alle_beziehungen);
    
     			$the_query->set('post__not_in', $alle_beziehungen);
     			
     			
     			///test Query dont update…:
    			$the_query->set('posts_per_page', 1);