Support

Account

Home Forums General Issues Distinct values from have_rows the_row Reply To: Distinct values from have_rows the_row

  • 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(); 
    	    }
    	}
    }