Home › Forums › Front-end Issues › Need to filter post object by one taxonomy
So, my whole post object with repeaters loop is working fine. HOWEVER, now I need to ONLY show items that are assigned to a specific taxonomy term. I’ve tried the following and it’s just not working:
<?php if( have_rows('cast') ):
while ( have_rows('cast') ) : the_row();
$cast_object = get_sub_field('actor_actress', 'cast_crew_category-83');
if( $cast_object ):
$thumbid = get_post_thumbnail_id($cast_object->ID);
$thumb = wp_get_attachment_image_src( $thumbid, 'thumbnail' );
$url = $thumb['0'];
$role = get_sub_field('role');
$term = get_terms( 'cast_crew_category' );
echo $role; echo $term->name;
<a href="<?php echo get_permalink($cast_object->ID); ?>" rel="lightbox" data-lightbox-type="iframe"><img src="<?php echo $url; ?>" width="100%" height="auto"></a>
<a href="<?php echo get_permalink($cast_object->ID); ?>" rel="lightbox" data-lightbox-type="iframe"><?php echo $cast_object->post_title; ?></a>
<?php endif;
endwhile;
endif;
It still shows all items in that post_object sub_field instead of only the ones assigned to that category.
The second parameter for get_sub_field()
is a boolean value that tells ACF whether or not to format the value. The string you entered evaluates to true, so no difference will be seen.
In order to do what you want you will need to something like
// ...
if ( $cast_object) {
// https://codex.wordpress.org/Function_Reference/wp_get_object_terms
$terms = get_object_terms($cast_object->ID, 'cast_crew_category', array('fields' => 'ids'));
if (in_array(83, $terms)) {
// ... the rest of your code here ...
}
}
// ...
This is what I get now:
Fatal error: Call to undefined function get_object_terms()
This is my current code:
if( have_rows('cast') ):
while ( have_rows('cast') ) :
the_row();
$cast_object = get_sub_field('actor_actress');
if ( $cast_object) {
$terms = get_object_terms($cast_object->ID, 'cast_crew_category', array('fields' => 'ids'));
$thumbid = get_post_thumbnail_id($cast_object->ID);
$thumb = wp_get_attachment_image_src( $thumbid, 'thumbnail' );
$url = $thumb['0'];
$role = get_sub_field('role');
if (in_array('83', $terms)) {
echo $role; ?>
<a href="<?php echo get_permalink($cast_object->ID); ?>" rel="lightbox" data-lightbox-type="iframe"><img src="<?php echo $url; ?>" width="100%" height="auto"></a>
<a href="<?php echo get_permalink($cast_object->ID); ?>" rel="lightbox" data-lightbox-type="iframe"><?php echo $cast_object->post_title; ?></a>
<?php }
}
endwhile;
endif;
The topic ‘Need to filter post object by one taxonomy’ is closed to new replies.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.