Home › Forums › General Issues › Distinct values from have_rows the_row
I have a question related to taxonomy terms and acf repeatable post object.
I have a terms from multiple taxonomies who has linked repetable field with post objects. There can be situations, when user can link similar post objects to different terms and that’s normal. If you imagine the situations, when user links wine to all Italian foods from taxonomy foodtype and also links wine to ingredients term pasta.
The problem is, I need to show on the single post (reciepe) only distinct values. Avoid the situation, when one reciepe gets similar related products from different taxonomies.
This is the code to show the related products on a single.php . I can not figure out of how to manipulate the have_row() or the_row() to show only distinct values. Do you have any clue?
global $post;
$taxes = array('ingredients','foodtype', 'eventtype');
$terms = get_the_terms($post->ID, $taxes);
if( !empty($terms) ){
foreach($terms as $term) {
if(get_field('relatedproducts', $term->taxonomy.'_'.$term->term_id )){
while( have_rows('relatedproducts',$term->taxonomy.'_'.$term->term_id) ) : the_row();
$post_object = get_sub_field('product');
if( $post_object ){
// override $post
$post = $post_object;
setup_postdata( $post );
if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail',array('class'=>"img-responsive center-block"));
}
the_title('<h4>','</h4>');
the_content();
}
}
wp_reset_postdata();
endwhile;
}
}
Hi!
I’m not 100% sure what you’re after but assuming what you want is to avoid duplicate related posts being displayed because they’re assigned to more than one term this code should do the trick.
It’ll create an array which it will check for each related post, if the posts slug is already in the array it’ll ignore it (and thus not display a duplicate) and if its not it’ll display the post and add it to the array.
global $post;
$taxes = array('ingredients','foodtype', 'eventtype');
$terms = get_the_terms($post->ID, $taxes);
$products = array();
if( !empty($terms) ){
foreach($terms as $term) {
if(get_field('relatedproducts', $term->taxonomy.'_'.$term->term_id )){
while( have_rows('relatedproducts', $term->taxonomy.'_'.$term->term_id) ) {
the_row();
$post_object = get_sub_field('product');
if( $post_object ){
// override $post
$post = $post_object;
setup_postdata( $post );
if(!in_array($post->post_name, $products)){
$products[] = $post->post_name;
if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail',array('class'=>"img-responsive center-block"));
}
the_title('<h4>','</h4>');
the_content();
}
}
}
wp_reset_postdata();
}
}
}
The topic ‘Distinct values from have_rows the_row’ 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.