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 request correctly, replace YOUR_FIELD_NAME_HERE with the field name of the relationship field you’ve created under the school_class custom post type.

    
    <?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
                // Refer to https://codex.wordpress.org/Function_Reference/has_category for more options
    			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;	
    
    ?>