Question

Combining taxonomies and user roles, I am listing all terms of tax "dog", and for each term "dog", a list of all "colors" that are linked to a user profile. EXAMPLE:

  • User 1 has meta "Dog: Golden Retriever" and "Color: Yellow" stored with profile.
  • User 2 has meta "Dog: Golden Retriever" and "Color: Black" stored with profile.
  • User 3 has meta "Dog: Golden Retriever" and "Color: Black" stored with profile.

Heading 3 tags below spits out the "term name" = which in this case would be 'dog'. Underneath dog, there should be a list of "colors" that are linked to this dog via user meta - IE, Yellow, Black. The important thing is that "Black" ONLY appears ONCE, not twice. I am trying to remove duplicates, but I am getting an error.

Edited: The error no longer persists. However, now - the echo $array only echos "Black", not "Yellow" or any other color.

Any thoughts?

<?php $terms = get_terms('dogs');
$count = count($terms);
if ( $count > 0 ){ foreach ( $terms as $term ) { ?>

<h3><?php echo $term->name; ?></h3> // Show different "dog" type names 

<div class="listed_dogs_color_names"> 

    // Now search all editor and contributor user profiles for "dog" and "color" user_meta. Color and dog are both taxonomies that are used both with posts and users (user meta)
    // If matches with the "dog" above, list "color" underneath "dog name" in <h3></h3> above.

<?php
$term_parent = $term->parent;
$term_slug = $term->slug;
$editor_query = new WP_User_Query(
    array(
    'role'         => 'editor',
    'meta_key'     => $term_parent, 
    'meta_compare' => '=', 
    'meta_value'   => $term_slug,       
)
);
$editors = $editor_query->get_results();
$contributor_query = new WP_User_Query(
    array(
    'role'         => 'contributor',
    'meta_key'     => $term_parent, 
    'meta_compare' => '=', 
    'meta_value'   => $term_slug,       
)
);
$contribs = $contributor_query->get_results();
$users = array_merge( $contribs, $editors ); 
?>

<?php 
                    $array = array(); // initialize as empty array ?>
                        <?php if (!empty($users)) {?>
                                <?php foreach ($users as $user) : 
                                        $b = $user->color;
                                        $color = explode("\n", $b);
                                        $array[] = $color[0];
                                        ?>
                            <?php endforeach; ?>

                            <?php $array = array_unique($array); ?>
                            <?php 
                            echo "<li>";
                            echo $array[0]; 
                            echo "</li>";
                            ?>
                        <?php } ?>

</div><!--close-->
<?php }?>
<?php }?>
Was it helpful?

Solution 2

Solution to the problem, many thanks to @Amal for walking through it with me

<?php if (!empty($users)) {?>
  <ul>
    <?php 
   $array = array();
   foreach ($users as $user) :  
       $b = $user->color;
       $color = explode("\n", $b);
       $array[] = $color[0];
    ?>

<?php endforeach; ?> // close the foreach, looped through all users already and stored meta into array

<?php $array = array_unique($array, SORT_REGULAR); ?> // take array and remove dupes                        

     <?php foreach ($array as $item) { ?> // foreach and style. can also implode and separate with commas via <?php $spitarray = implode ( ", ", $spitarray); ?>
          <li><?php echo $item; ?></li>
     <?php } ?>
  </ul>
<?php } ?>

OTHER TIPS

By default, PHP will try to convert the objects into strings for comparison. This is causing the error. You can specify the second parameter of array_unique() to override this:

foreach(array_unique($users, SORT_REGULAR) as $user)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top