3

I have a Problem with grouping meta_values. The Query looks for posts with the metakey "company". I want a unique listing of the colors like: blue red yellow

array_unique was not successful and also custom mysql query.

<?php
$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;
?>

<?php echo get('color'); ?> 
// Outputs yellow yellow blue yellow red yellow

<?php endwhile; ?>

Current output is: yellow yellow blue yellow red yellow

Thanks.

Edit:

Thanks for the help!!!

This is the final working code:

<?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;
$colors[] = get('color');

// Creates an array of all colors

endwhile;
$colors = array_unique($colors);
// Removes duplicates;
foreach($colors as $color){
echo $color.' ';
} ?>
mistertodd
  • 85
  • 2
  • 9

2 Answers2

3
while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;
    $colors[] = get('color'); 
    // Creates an array of all colors

endwhile;
$colors = array_unique($colors);
// Removes duplicates;
foreach($colors as $color){
    echo $color;
}
Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78
RST
  • 3,899
  • 2
  • 20
  • 33
  • Nope, still got duplicated colors. :-( I don´t understand it. Why isn´t this working? – mistertodd Jul 10 '13 at 09:48
  • Now it´s working!!!! Have changed the line `array_unique($colors);` to `$colors = array_unique($colors);` – mistertodd Jul 10 '13 at 09:51
  • It may be a good enough solution to your problem, but, imagine the situation of taking 10000 rows from database just to see that there are three color groups. Should, if possible, be better done in mysql side :) – Andrius Naruševičius Jul 10 '13 at 11:23
  • Have you a mysql solution? That would be great! – mistertodd Jul 10 '13 at 12:40
  • Take a look here http://stackoverflow.com/questions/8571902/mysql-select-only-unique-values-from-a-column – RST Jul 10 '13 at 22:47
  • This is a terrible answer for the following reasons: 1) it's results in running a bigger query than is needed. 2) Additional work is also needed to then filter the results in an order needed. 3) Said additional filtering is prone to edge case errors. The real answer would be to modify the original wp_query in a way that provides the desired results. This answer is just lazy and not a real solution. – Hybrid web dev Jul 27 '21 at 00:28
0

There is a SQL solution: SELECT DISTINCT ..., but I think that doesn't work in WP_QUERY but you can use $wpdb directly. Here is an example https://gist.github.com/tybruffy/6253428. (Be sure to query the database prefix because it is not always 'wp_').

erdmann
  • 11
  • 2