I'm trying to build a filter for a custom post type page. The custom post type has a metabox that allows inputting of different authors.

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;
foreach( $authors as $author ):
<option value="<?php echo $author;?>"><?php echo $author;?></option>
<?php endforeach; ?>

The problem is that some values are grouped together, typed separated by commas. I need to take every value in the field as individual, and avoid duplicates.

1 Answer 1


This is just a basic php array question - not a WP question.

function unique_authors ( $authors ) {
    $newArray = array();

    foreach( $authors as $item ) {
        $itemArray = explode( ", ", $item );
        $newArray = array_merge($newArray, $itemArray);

    $newArray = array_unique($newArray);
    return $newArray;

$authors = unique_authors( $authors );

foreach( $authors as $author ):  //etc
  • The problem is that my dropdown is displaying the authors as entered in the custom metabox that allows different values so some will display: Name1, Name2, Name 3 in a line, then another line with Name 1 then another line with Name 2 and so on if people chose only one in the meta value. But the issue is with multiple ones - need to break them into separate values and not repeat if used in different combinations :O Jul 21, 2020 at 20:25
  • Yikes. So the real problem is the metabox. Forcing people to input uniform data is always tough. You could use jquery to show a single input field + a button to add another author input field. Juggling the data after input is never very feasible.
    – shanebp
    Jul 21, 2020 at 21:56
  • I have a metabox that allows the user to select published posts from a custom post type. The metabox itself needs to behave this way. The user selects from posts there. I need to break the multiple posts selected there to different items in my query :c Jul 21, 2020 at 23:55
  • Show us the data that you need to process into 'different items'
    – shanebp
    Jul 22, 2020 at 1:59
  • How do I show you the data? I tried using the print_r but it doesn't show anything at all :/ Jul 22, 2020 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.