WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom query filtered with GROUP BY not working (3 posts)

  1. edwinbradford
    Member
    Posted 3 years ago #

    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?

  2. edwinbradford
    Member
    Posted 3 years ago #

    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...

  3. kmvan
    Member
    Posted 2 years ago #

    StackOverflow ?
    would i get the link about the question?

Topic Closed

This topic has been closed to new replies.

About this Topic