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.

Using the following code from this post I thought I had found the answer to my problems of showing authors that have posted in the last 6 months. Unfortunately the way the function checks the date it only seems to block some months and doesn't take in to consideration previous years as it is showing older posts/authors still.

I believe the problem lies in the if (6 < (date('n',(time() - strtotime($posts[0]->post_date))))) with the n being the month.

Could anyone assist to correct the lookup so it only shows from the last 6 months?

<?php
// Arguments to pass to get_users
// ************* $args = array( 'orderby' => 'post_count', 'order' => 'DSC', 'who' => 'authors' );
// Query for the users
$authors = get_users('orderby=post_count&order=DSC'); //&role=contributor ?>
<?php
// Loop through all the users, printing all of their posts as we go
foreach ( $authors as $author ) {

    // 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);

    if (6 < (date('n',(time() - strtotime($posts[0]->post_date)))))
        continue; //skips this autor as long as his last post is older than 6 months, be aware that this check uses date function.

 ?>
            <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 if ( have_posts() ) :  ?>
                <div 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 - for now the title ?>
                    <div class="author-descrip" style="padding-bottom:2px;">
                        <?php the_author_meta("description"); ?></div><br />
          <p><strong>Most recent article: </strong><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></p>
          <p>
            <?php if(get_the_author_meta('user_url')): ?>
                <?= get_the_author_meta('user_url') ?>
            <?php endif; ?>
          </p>
        <?php endwhile; ?>
                </div><!-- #author-post-list -->
    <?php else: ?>
                    <p style="font-style:italic;">This author has not yet published any posts</p>
    <?php endif; ?>
                </div><!-- #author-posts -->
            </div><!-- #author-posts-wrapper -->
<?php } // End looping over all users ?>
share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

You might want to use something like this:

if (strtotime('-6 months', time()) >= strtotime($posts[0]->post_date))
    continue;

When using strtotime, you actually can calculate in words, (e.g., +1 month, -3 hours).

share|improve this answer
    
This seems to have the opposite effect and show only people who have posted longer than 6 months ago? –  Vince Pettit Apr 17 '13 at 14:33
    
Yes, you're right. I didn't test your whole code, as you can imagine, and mixed the cases here (where a continue is used). Now it should do. –  t f Apr 17 '13 at 14:36
    
Perfect, thanks! –  Vince Pettit Apr 17 '13 at 14:44
add comment

strtotime can do date math.

echo date('Y m d',strtotime('-6 months',time()));

So you should be able to do something like...

$ptime = strtotime($post->post_date);
$min6m = strtotime('-6 months',time());
if ($ptime <= $min6m) { 
  // echo 'yay';
  continue;
}
share|improve this answer
    
This doesn't work for some reason, I get no results at all –  Vince Pettit Apr 17 '13 at 14:29
    
Not when I test it, and the logic is correct (unless I am going insane). $ptime (post time) is greater than or equal to $min6m (minus 6 months). –  s_ha_dum Apr 17 '13 at 14:36
1  
@s Yes, our logic is correct, but here, a continue is used in the true case. Didn't get that before, too. ;) –  t f Apr 17 '13 at 14:37
    
Ok... same mistake as @tf. Fixed it. I think. So confused... :) –  s_ha_dum Apr 17 '13 at 14:38
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.