WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Query post to show one post per author ? (18 posts)

  1. SimonJ
    Member
    Posted 4 years ago #

    Anybody have an idea how to make loop with a query posts with 1 post per author ?

    I mean, display a number of post on the front page, with only the last post from a given author... i.e. an author cannot have more than one post on the front page ?

    Thanks

    S,

  2. Shane G
    Member
    Posted 4 years ago #

    Hi,

    I suggest you to do it with the database query and you can display result on your home..rather the query_post...database query will be much easier to complete your requirement..

    Thanks,

    Shane G.

  3. MichaelH
    Member
    Posted 4 years ago #

    Try this:

    <?php
    //get all users, iterate through users, query for one post for the user,
    //if there is a post then display the post title, author, content info
    $blogusers = get_users_of_blog();
    if ($blogusers) {
      foreach ($blogusers as $bloguser) {
        $args = array(
        'author' => $bloguser->user_id,
    	  'showposts' => 1,
    	  'caller_get_posts' => 1
        );
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          // $user = get_userdata($bloguser->user_id);
          // echo 'This is one post for author with User ID: ' . $user->ID . ' ' . $user->user_firstname . ' ' . $user->user_lastname;
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    
            <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?> </small><?php
            the_content();
          endwhile;
        }
      }
    }
    ?>

    These two comments lines left just in case you want additional info about each user:

    // $user = get_userdata($bloguser->user_id);
    // echo 'This is one post for author with User ID: ' . $user->ID . ' ' . $user->user_firstname . ' ' . $user->user_lastname;
  4. SimonJ
    Member
    Posted 4 years ago #

    Ahhhh.... get_users_of_blog(); !... Seems like the abracadabra I was looking for!

    I'll give it a try and get back to you with feedback asap.

    S.

  5. SimonJ
    Member
    Posted 4 years ago #

    Nice nice nice!

    Exactly what I was looking for!

    Thanks x 1000 MichaelH!

    S.

  6. SimonJ
    Member
    Posted 4 years ago #

    Hummm

    One last question :

    This query seems to always order the posts by author id...

    Is there a solution to order by date ?

    I tried 'orderby' => date, in $args with no succes...

    Thanks

    S.

  7. mikem1986
    Member
    Posted 4 years ago #

    I would like to do something similar to this but only have 1 of each category on the page...

  8. MichaelH
    Member
    Posted 4 years ago #

    It is not the query posts that is ordering the posts, it is the get_users_of_blog() that returns the array of users.

    If you want that, guess there are several ways to do that--one idea would be to save the post IDs in an array, then use the post__in parameter with that array.

    Or use wpdb and get a results array of the posts, one for each author sorted by post_date.

  9. MichaelH
    Member
    Posted 4 years ago #

    mikem1986 - please start another topic with that question.

  10. SimonJ
    Member
    Posted 4 years ago #

    Hum... I posted a solution here, but obviously, I still have to scratch my head... :-)

    Sorry... :-)

    S.

  11. MichaelH
    Member
    Posted 4 years ago #

    Hum... I posted a solution here, but obviously, I still have to scratch my head... :-)

    Posted what, where?

  12. SimonJ
    Member
    Posted 4 years ago #

    I posted the solution I found, but after a last test, it did'nt work... So I edited my post... :-)

    But now, I think I found a solution... Seems to work...

    Since I'm almost a php/sql analphabet, I didn't understand very well your last answer, especially the first idea with "post__in".

    So here is what I did :

    I made a function, on the same principle than "get_users_of_blog"...

    function the_last_post_per_user(){
    global $wpdb;
    $the_authors = $wpdb->get_results( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_status='publish' AND post_type='post' AND post_author !='1' ORDER BY post_date DESC LIMIT 0,10");
    return $the_authors;
    }

    I use the post table to retreive the authors Ids... this way, I'm able to order by post date... I made a "Select distinct" query to avoid duplication of post_author Ids...

    Then, I use this function before the query you gave me yesterday, instead of "get_users_of_blog"... And I replace the $bloguser->user_id by : $bloguser->post_author :

    <?php
    //get all users, iterate through users, query for one post for the user,
    //if there is a post then display the post title, author, content info
    $blogusers = the_last_post_per_user;
    if ($blogusers) {
      foreach ($blogusers as $bloguser) {
        $args = array(
        'author' => $bloguser->post_author,
    	  'showposts' => 1,
    	  'caller_get_posts' => 1
        );
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
    
     [...]
    
          endwhile;
        }
      }
    }
    ?>

    Seems to work, so far...

    Do you have any opinion, or see any problem about this solution Michael? I would be glad if you can give me some feedback about it...

    S.

  13. MichaelH
    Member
    Posted 4 years ago #

    Very nice solution. Good job.

    I will mark this resolved.

    Thanks for the solution/feedback.

  14. beccca
    Member
    Posted 4 years ago #

    This solution is so close, but I need it to go one step further and would be so grateful for any thoughts..

    I would like to run a query through WordPress that displays 4 recent posts on the site that are filtered by only displaying one post per author. I also want the results to filter the users so only posts by authors or editors show up.

    Thanks in advance!

  15. wakeless
    Member
    Posted 4 years ago #

    I've just submitted a plugin that does this.

    http://wordpress.org/extend/plugins/one-post-per-author-per-page/

  16. tenganmade
    Member
    Posted 4 years ago #

    Just thought I would share my edit of this code which is more optimized.

    function last_post_per_user()
    {
    	global $wpdb;
    
    	return $wpdb->get_results('SELECT post_author, post_title FROM '.$wpdb->posts.' WHERE post_status=\'publish\' AND post_type=\'post\' AND post_author!=\'1\' GROUP BY post_author ORDER BY post_date DESC');
    }

    This collects one post per author ordered by the post's date.
    The GROUP BY attribute in SQL is the magic thing here.

    Then all that's needed is an associative loop.

    $posts = last_post_per_user();
    
    foreach ($posts as $post)
    {
    	// Code goes here, $post contains the post variables post_author and post_title (which was what I needed for this).
    	echo $post->post_title,'<br />';
    }
  17. tenganmade
    Member
    Posted 4 years ago #

    Correction; my previous funtion doesn't order by date the way intended.

    function last_post_per_user()
    {
    	global $wpdb;
    
    	return $wpdb->get_results('SELECT post_author, post_title FROM '.$wpdb->posts.' INNER JOIN (SELECT MAX(ID) AS id FROM '.$wpdb->posts.' WHERE post_status=\'publish\' AND post_type=\'post\' AND post_author!=\'1\' GROUP BY post_author) latest ON wp_1_posts.ID = latest.id');
    }

    This SQL statement returns one item per author, sorted by the highest post ID for that user.

  18. smallwei
    Member
    Posted 4 years ago #

    I have found the simplest solution without using plugin and other custom loops. In case someone is interested, see the instruction here:
    http://www.dbuggr.com/smallwei/show-latest-post-author-wordpress-front-page/

Topic Closed

This topic has been closed to new replies.

About this Topic