0

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;
4
  • 2
    Does this answer your question? How to break meta values into different items and avoid duplicates?
    – shanebp
    Commented Jul 24, 2020 at 22:02
  • Hi shanebp, I did make that question trying to solve this problem - however I figured out I was calling the wrong metabox so you answered that question that had written names separated by commas (facepalm). I thought of asking another question instead, in case people need that question answered. This metabox (the correct one) has custom posts and the IDs are repeating if chosen in different posts, I need no duplicates Commented Jul 26, 2020 at 15:59
  • The answer I gave describes an approach to removing duplicates. The answer or parts of it should solve your issue.
    – shanebp
    Commented Jul 26, 2020 at 18:50
  • The answer helps with data separated by commas in the same array, but hmm in this one I have different arrays with repeated IDs I need to check if it is already in the array then dont repeat it ... I tried doing a new array with the data then check if it is there but its not working :c Im sorry Im kind of noob Commented Jul 27, 2020 at 5:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.