Custom query filtered with GROUP BY not working
-
I’m trying to get the posts from a Custom Post Type and list the contained custom fields as types so that if I have for example four posts with the custom field “fruit” set to “banana” I get a list showing one “banana” and not “banana” listed four times. Its to build a legend, a key, for the content.
From what I’ve been reading the best way to do this is using
GROUP BY
in a filter around my custom query but I can’t seem to get it to do anything and don’t know how to bug test query calls to the database.I tried StackOverflow but no response.
My code is…
function sort_legend ( $groupby ) { global $wpdb; $groupby .= "{$wpdb->postmeta}.meta_key = 'Color' "; return $groupby; }
…in my functions.php and…
<?php add_filter('posts_groupby', 'sort_legend'); ?> <?php if ( $myQuery->have_posts() ) while ($myQuery->have_posts() ) : $myQuery->the_post(); ?> <?php remove_filter('posts_groupby', 'sort_legend'); ?>
…around my custom query.
Any suggestions, or does anyone think there’s a simpler way to do it?
- The topic ‘Custom query filtered with GROUP BY not working’ is closed to new replies.