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;
?>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.