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 have several writers that range from frequent to one-time writers for my blog. On my Author page, I would like to separate out writers that have not posted an article in 6 months. It would be nice then to output those writers in a different section like "Previous Writers" or something after the other list of current writers.

Right now, the code just loops through all authors and displays them all together so I would like to add in this exclusion based on the last time they posted an article.

Specific code help is welcomed as I don't know PHP or Wordpress extremely well.

Example of code:

$authors = get_users('orderby=post_count&order=DSC&role=contributor'); ?>
<h4 style="padding-top:20px;">Writers</h4>
<?php
// Loop through all the users, printing all of their posts as we go
foreach ( $authors as $author ) { ?>
            <a name="<?php echo $author->user_nicename; ?>"></a>
            <div class="author-posts-wrapper" id="author-<?php echo $author->ID; ?>-posts-wrapper">
                <div class="author-avatar" id="author-<?php echo $author->ID; ?>-avatar">
                    <?php echo get_avatar( $author->ID, 96 ); ?>
                </div>
                <div class="author-posts" id="author-<?php echo $author->ID; ?>-posts">
                    <h2><a href="<?php echo get_author_posts_url( $author->ID ); ?>"><?php echo $author->display_name; ?></a></h2>
    <?php
    // Set up a Loop, querying for all of the current user's posts
    $args = array( 'author' => $author->ID, 'posts_per_page' => 1 );
    $posts = query_posts($args);
    // Now that we have the posts, simulate a Loop here
    if ( have_posts() ) : ?>
                <ul class="author-post-list" id="author-<?php echo $author->ID; ?>-post-list">
        <?php while ( have_posts() ) : the_post(); // Print whatever we want for each post ?>
        Output something about each writer here.
        <?php endwhile; ?>
                </ul><!-- #author-post-list -->
    <?php else: ?>
           <p style="font-style:italic;">This author has not yet published any posts</p>
    <?php endif; ?>

In case you want to see the full code, the Author page was built using this code.

share|improve this question
add comment

1 Answer

So far as I know there is no 'last posted on' value saved for a user. You can find that information by referencing the post_date in the *_posts table against the author data in *_users and *_usermeta. With that in mind I can think of several solutions.

First Solution:

  1. Pull your authors. You need this later.
  2. Pull all of your published posts.
  3. Loop through the posts sorting things by author and keeping track of the most recent post_date for each author.
  4. You can then loop through the sorted posts and print them out.

Here are the problems:

  1. You are pulling all of your published posts. This could potentially be a very heavy, slow page, and could even time out or run out of memory. The upside is that your existing code pulls all of your published posts as well but does so with a lot more database queries, which is usually but not always worse than one big query.
  2. You have to loop through all of your posts in order to sort them and then loop through again to print them. Again, heavy, slow, and may have execution timeout or memory problems. Again, you are more or less forced into this if you want to do what you are trying to do.

Second Solution:

Another solution would be to run a query to pull the latest post for each user ID, use that to sort the author data, then use your existing code to print them. I believe you will need to write the SQL for that query. I don't think there is anything built in to do it. It may not have as many memory problems as the first solution but it is adding a query to the several queries you already have.

Third solution:

Still another solution would be to be to loop through your posts and add a 'last posted' value to *_usermeta for each author. You'd have to update this whenever a post date is changed or a post published. You could then use the meta parameters of get_users to sort on that 'last posted' field. This is more efficient than the immediately preceding solution but is more complicated. It still requires several trips to the database.

As you can imagine, there is a fair chunk of code, and debugging time, to any of these solutions. Apologies for not providing it. I don't think I could get it right without actually implementing the several solutions and debugging them.

Maybe an alternative:

Another thing you might consider is doing your server, and your visitors, a favor by not printing posts for all authors on the same page, since that really works out to printing all published posts on the same page plus sorting overhead. For example, print an author 'index' page sorted by last post date if you want, with a link to an author detail page that has the posts on it (which is fact is very similar to what happens by default-- authors show up at "/author/author-name").

Also, you shouldn't be using query_posts. It clobbers the main query. Use get_posts instead.

share|improve this answer
    
Following up on your suggestion of an "Author index", that is basically what the page is intended to do. This is suppose to serve as a Staff page where it merely shows Author names, bio, and the title of their last post then a link off to their own separate author page. I would just like to separate the Staff that have not posted in a long time. Does that help at all? –  Dustin J Nov 12 '12 at 16:41
    
Here is the live example of the Staff page I am talking about. Staff page link –  Dustin J Nov 12 '12 at 16:43
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.