Hi,
I have 2 custom posts types:
1 – subject ( has categories: easy, normal, hard)
-name (text)
-teacher (text)
2 – school_class (no categories)
-subject (relationship)
-place (text)
I need to know how to get all the school_class that have the “easy” category (of subject).
Thanks
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;
?>
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;
?>