admin管理员组文章数量:1327936
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;
}
endwhile;
wp_reset_postdata();
endif;
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.
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;
}
endwhile;
wp_reset_postdata();
endif;
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.
Share Improve this question edited Jul 24, 2020 at 17:11 artist learning to code asked Jul 21, 2020 at 19:55 artist learning to codeartist learning to code 3311 gold badge5 silver badges18 bronze badges1 Answer
Reset to default 1This 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
本文标签: custom fieldHow to break meta values into different items and avoid duplicates
版权声明:本文标题:custom field - How to break meta values into different items and avoid duplicates? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742234978a2437912.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论