AnswerBun.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!

Related Questions

Plugin updates are not shown

1  Asked on December 24, 2021

   

WordPress Customizer Help with FontAwesome Icon

2  Asked on December 24, 2021 by ashiquzzaman-kiron

     

Move users and passwords from one wordpress site to another

3  Asked on December 24, 2021 by user106747

     

adding fade on mouseover to images

2  Asked on December 24, 2021 by baksudin

     

Get post terms with hierarchical relationships

1  Asked on December 22, 2021 by mikkelbreum

   

All links redirecting to homepage

1  Asked on December 22, 2021 by thebeardedguy

 

Simple Local Avatar Plugin

1  Asked on December 19, 2021 by user34634

       

Disable plugins on cron and ajax page

1  Asked on December 19, 2021 by humpty

   

Ask a Question

Get help from others!

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