How to break meta values into different items and avoid duplicates?
I’m trying to build a filter for a custom post type page. The custom post type has a metabox that allows choosing multiple authors but those are another custom post. I need this to be kept as a separate custom post type (rather than users) because of other functions not relevant to the question.
I am displaying the post names in a dropdown like this, using the get_post_meta:
/*Get the Authors*/
$list_authors = array(
'post_type' => array('publications'),
);
$authors = array();
$query_authors = new WP_Query( $list_authors );
if ( $query_authors->have_posts() ) :
while ( $query_authors->have_posts() ) : $query_authors->the_post();
$author = get_post_meta(get_the_id(), 'cl_pub_authors', true);
if(!in_array($author, $authors)){
$authors[] = $author;
}
endwhile;
wp_reset_postdata();
endif;
foreach( $authors as $author ):
?>
<option value="<?php echo $author;?>"><?php echo $author;?></option>
<?php endforeach; ?>
The problem is that if a user chooses the same ‘author’ on a different post the value is duplicated, and also some values are grouped together, since they were chosen in that post as authors. I need to take every value in the field as individual, and avoid duplicates.
Totally lost here so anything is helpful! 😀
Leave an answer