Support » Fixing WordPress » Query post to show one post per author ?

  • Resolved SimonJ

    (@simonj)


    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,

Viewing 15 replies - 1 through 15 (of 17 total)
  • 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.

    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;

    Thread Starter SimonJ

    (@simonj)

    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.

    Thread Starter SimonJ

    (@simonj)

    Nice nice nice!

    Exactly what I was looking for!

    Thanks x 1000 MichaelH!

    S.

    Thread Starter SimonJ

    (@simonj)

    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.

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

    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.

    mikem1986 – please start another topic with that question.

    Thread Starter SimonJ

    (@simonj)

    Hum… I posted a solution here, but obviously, I still have to scratch my head… 🙂

    Sorry… 🙂

    S.

    Hum… I posted a solution here, but obviously, I still have to scratch my head… 🙂

    Posted what, where?

    Thread Starter SimonJ

    (@simonj)

    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.

    Very nice solution. Good job.

    I will mark this resolved.

    Thanks for the solution/feedback.

    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!

    I’ve just submitted a plugin that does this.

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

    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 />';
    }
Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Query post to show one post per author ?’ is closed to new replies.