Need to Remove Duplicate Posts in a Foreach Loop

717 views Asked by At

I'm working on a site where major categories have different professions registered under them. When you click on a particular category (let's take Music for example), you will see all professionals registered under that category. To achieve this, I created a user taxonomy called Profession. A user can have more than one profession under a category. Whenever I query akk professionals in a category, I get duplicate posts which I can't seem to stop. Here is my foreach loop:

   <?php

    $users = get_objects_in_term( array(104, 106), 'profession' ); // there are too user ids listed in the array

    if ( !empty( $users ) ) {
     foreach ( $users as $user_id ) { 
   ?>

  <div>
      ...HTML CODES...
  </div>
  <!-- End Loop -->

      <?php } 
  } 
  else { ?>
    <p><br><br><?php _e('No registered user in this profession.'); ?></p>
  <?php } ?>

Thanks in advance

1

There are 1 answers

1
Alejandro Sanchez On BEST ANSWER

You need to use the PHP function array_unique to remove your duplicated user id's, like this:

$users = array_unique(get_objects_in_term( array(104, 106), 'profession' ));

The rest of your code will work just fine.