TransWikia.com

How to avoid duplicate posts queried from custom meta?

WordPress Development Asked on January 2, 2022

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;

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP