WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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";


share|improve this question
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.

share|improve this answer

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.


share|improve this 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 posting your answer, you agree to the privacy policy and terms of service.

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