Support

Account

Home Forums General Issues Distinct values from have_rows the_row

Solved

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(); 
    	    }
    	}
    }
    
    
  • Wow, that just worked out! Thanks!

  • Great 🙂

    Good luck in the project!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.