How to avoid duplicate posts queried from custom meta?

Question

I’m querying custom posts taken from a custom metabox where you pick which posts to display to be used as criteria for filtering. How do I avoid duplicates?

Current code (displays all authors – the custom post type selected in metabox – but if more than one post selected the same, then it is repeated.. need one instance only for each):

$list_authors = array(
'post_type'         => array('publications'),
);
$query_authors = new WP_Query( $list_authors );
if ( $query_authors->have_posts() ) :
while ( $query_authors->have_posts() ) : $query_authors->the_post();
/*Get the Authors*/
$unique_authors = array();
$author_ids = get_post_meta(get_the_id(), 'cl_pub_auth', true);
$author_ids = ( $author_ids && is_array( $author_ids ) ) ? array_map( 'intval', $author_ids ) : array();

if ( $author_ids ) {
foreach ( $author_ids as $author_id ) {
if(!in_array($author_id, $unique_authors)):
$author_id[] = $author_id;
endif;
$authors[] = get_post( $author_id );
}
}

endwhile;
wp_reset_postdata();
endif;
0
, , , Cynthia Lara 1 year 2020-07-24T15:10:49-05:00 0 Answers 49 views 0

Leave an answer

Browse
Browse