Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

<?php
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";
...

Thanks

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

2 Answers 2

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.

EDIT

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.

http://codex.wordpress.org/Custom_Queries

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

 
discard

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.