posts_groupby problem

Your filter will result in SQL query with GROUP BY statement but without aggregate function. This will result in displaying only the first row that appear in the group that match your query and omitting the subsequent rows. Read this question for more details.

If you want to order your results by post_type, you can hook to posts_orderby filter instead:

add_filter('posts_orderby', 'group_by_post_type', 10, 2);
function group_by_post_type($orderby, $query) {
    global $wpdb;
    if ($query->is_search) {
        return $wpdb->posts . '.post_type DESC';
    }
    // provide a default fallback return if the above condition is not true
    return $orderby;
}

Leave a Comment