This is the solution, according to the asker. He used the get()
method of the WP_Query
class to retrieve an array of values (colors), each stored with the meta key color
. Because [color] => 'Yellow'
may appear in the array more than once, he then filtered the array through the PHP function, array_unique()
which will only return the first encounter of [color] => 'Yellow'
and ignore any subsequent keys with a value of Yellow
. The first encounter is returned for each value and you have a new array with no duplicate values.
Alternatively, if you’re just handling string values in your array, you may consider something like array_flip( array_flip ( $colors ) )
which is faster than array_unique()
.
<?php
$current_page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$cat_name = get_category(get_query_var('cat'))->name;
$args = array(
'category_name' => $cat_name,
'posts_per_page' => '60',
'paged' => $current_page,
'meta_query' => array(
array(
'key' => 'company',
'value' => 'microsoft',
'compare' => 'like'
)
)
);
$my_query = new WP_Query($args);
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
// Retrieve and store an array of values for the meta_key 'color'.
// This array may contain duplicate values.
$colors[] = get('color');
endwhile;
// Filter the array to keep only the first key encountered of each unique value.
$colors = array_unique($colors);
// Alternatively, you could replace the above line with a double flip like so:
// $colors = array_flip( array_flip ( $colors ) );
// The array contains no duplicate values at this point.
foreach($colors as $color){
echo $color.' ';
} ?>