• Resolved edwinbradford

    (@edwinbradford)


    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?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter edwinbradford

    (@edwinbradford)

    I solved this, although it was painful. Please see the link to StackOverflow above for the solution, I’m adding it now. As I read elsewhere, some days you’ll love WordPress, some days you’ll hate it…

    StackOverflow ?
    would i get the link about the question?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom query filtered with GROUP BY not working’ is closed to new replies.