Support

Account

Home Forums General Issues Get posts with a relationship post category Reply To: Get posts with a relationship post category

  • Assuming I’ve understood your post correctly, replace YOUR_FIELD_NAME_HERE with the field name of your relationship field.

    
    <?php 
    
    	// Get all school_class posts
    	$args = array( 
    		'post_type' => 'school_class', 
    		'posts_per_page' => -1,
    	);		
    
    	// Generate the loop
    	$loop = new WP_Query( $args );
    
    	while ( $loop->have_posts() ) : $loop->the_post();
    
    		// Get the relationship field_name that you've defined
    		$posts = get_field('YOUR_FIELD_NAME_HERE');
    
    		if ( $posts ):
    
    			foreach ( $posts as $post):
    
    				// Set up the post for posts with that field_name
    				setup_postdata($post); 
    
    				// Narrow the posts by the "easy" category
    				if (has_category('easy')):
    
    					// Print the whole post object
    					// Do what you want from here
    					print_r($post);
    
    				endif;
    
    			endforeach;
    
    			// Reset the postdata so the page works correctly
    			wp_reset_postdata();
    
    		endif;
    
    	endwhile;	
    
    ?>