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.

We are introducing a "featured author" area on our site and would like to display the most recent articles by a select group of authors. However, we only want a maximum of one post per author to display. So an author could potentially have posted 5 times since another author had posted, but regardless only one of their posts should appear. Currently this is the code I've got:

<?php
$args = array(
'showposts' => 5,
'author' => "6800,3845,1720,7045,4949"
);

$the_query = new WP_Query( $args );

while ( $the_query->have_posts() ) : $the_query->the_post(); 

?>

// DISPLAYING STUFF

<?php endwhile; wp_reset_query(); ?>

One potential solution I have considered is querying more posts and setting up an array, then checking the array each time to see if an author is already in it. If they are, it would continue to the next row. But an obvious issue with this would be that I may potentially end up having to pull back 100's of posts if a particular "featured author" hadn't wrote for a while.

I'm still fairly new to PHP/MySQL and a solution is probably starring me in the face. Appreciate any assistance.

share|improve this question
add comment

2 Answers

You need to GROUP BY the author ID, which is going to require a filter on posts_groupby. The Codex page for that filter is non-existent but it works like posts_join. Something like...

function filter_authors($groupby) {
  global $wpdb;
  $groupby = " {$wpdb->posts}.post_author";
 return $groupby;
}
add_filter('posts_groupby','filter_authors');

$args = array(
 'showposts' => 3,
 'author' => "1,2,3"
);

$the_query = new WP_Query( $args );

while ( $the_query->have_posts() ) : $the_query->the_post();
 echo $post->post_title.' :: '.$post->post_author."<br/>";
endwhile;

Use your own values in $args of course.

that will effect any other query on the page that runs after this block of code. You might want to remove the filter after you are done.

remove_filter('posts_groupby','filter_authors');
share|improve this answer
    
Great thanks, appreciate that. It worked, but seems to be showing each authors oldest (rather than newest) post. I have tried putting an orderby parameter into the args, but I'm guessing the filter is overriding it as it's having no effect. I have discovered another filter called posts_orderby, which has more documentation than the posts_groupby function, but I'm not sure if this is what I need. I tried implementing the filter but it caused an error. –  Peter Willis III Jan 3 '13 at 16:42
1  
Yeah, when you start messing with those filters you really have to know SQL since are literally just injecting raw SQL into the query. I know I can do this with pure SQL. Let me think about how to do it with WP_Query –  s_ha_dum Jan 3 '13 at 21:58
    
I was wondering if you had any thoughts on this? While the other answer in this thread does "work", your solution is cleaner if we can make it show the "latest" post rather than the first post. I've tried a few different methods to "filter" the results further to get the ordering to show the latest post without success. –  Peter Willis III Jan 9 '13 at 16:32
add comment

Another approach could be to use a function that loops through get_posts and prints the result.

Check this Q&A: When should you use WP_Query vs query_posts() vs get_posts()?

// function located in the theme's functions.php
function wpse_78117_print_authors_last_post() 
{
    $user_ids = array( '1', '2' );
    foreach( $user_ids as $user )
    {
        $args = array( 
                'post_type'     => 'post',
                'numberposts'   => 1,
                'author'        => $user
            ); 

        // as we are getting only 1 post, extract it from the returned array
        $user_post = array_shift( get_posts( $args ) );

        // similar
        $nick = array_shift( get_user_meta( $user, 'nickname' ) );

        // custom output, $user_post contains all the post normal data
        echo $user_post->post_title . ', by: ' . $nick;
    }
}

And then use it in any theme template:
<?php wpse_78117_print_authors_last_post(); ?>.

share|improve this answer
    
Isn't this approach more heavier for the server? –  Christine Cooper Jan 4 '13 at 13:10
    
@ChristineCooper, well, it does n queries, if there's a solution with only 1, then yes... Check this: wordpress.stackexchange.com/q/78218 –  brasofilo Jan 4 '13 at 15:50
add comment

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.