I display one post per author with function below and works fine, but is possible order by custom field?

add_filter('posts_groupby', 'author_groupby');
function author_groupby($groupby)
 global $wpdb;
 $author_groupby = "{$wpdb->posts}.post_author";
    if(preg_match("/$author_groupby/", $groupby)) {
    // grouping we need is already there
    return $groupby;
  if(strlen(trim($groupby) == 0)) {
    // groupby was empty, use ours
    return $author_groupby;
  return "$groupby, $author_groupby";

I've tried with code below but do not works:

$author_groupby = "{$wpdb->posts}.post_author 
AND pm.meta_key = 'KEY' 
ORDER BY pm.meta_value DESC Limit 0,1";


  • Can you explain what you are trying to do in more detail? What is the result you want in human terms?
    – s_ha_dum
    Dec 16 '12 at 22:37

You use a AND statement after a GROUP BY statement. That wont work.

Try altering the WHERE statement (posts_where_paged filter) to add the AND pm.meta_key = 'KEY' line.

Additionally you can use the posts_orderby filter to alter the ORDER BY.


You can find some additional information here.


There is a posts_orderby filter that works like the posts_groupby filter you are already using. I don't see the point of trying to write the code since I don't know what your key is.

To make the orderby work you may need filters on posts_fields and/or posts_joins as well.

I don't know that your question is precise enough to give a better answer.


  • I would show one post per author and order by post meta value. Example: (1) Post by author XXX with post meta 150 (2) Post by author YYY with post meta 100 (3) Post by author ZZZ with post meta 50 ...
    – Marco
    Dec 17 '12 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.