0

This question already has an answer here:

I am trying to get whether the author has posted within the last 30 days but this returns TRUE even if the author hasn't posted within the last 30 days.

      <?php $args = array(
'post_type' => 'post',
'post_status' => 'publish',
'author' => get_current_user_id(), 
'date_query' => array(array('after' => date('Y-m-d', strtotime('-30 days')), 'inclusive' => true,),)
, 'fields' => 'ids',);
$wp_posts = get_posts($args);
if(count($wp_posts)) : ?>
        <div>Author posted within the last 30 days</div>
      <?php else : ?>
       <div>Author did not post within the last 30 days</div>
      <?php endif; ?>

Not sure what I am doing wrong here.

marked as duplicate by Krzysiek Dróżdż, Nathan Johnson, Rick Hellewell, Howdy_McGee Feb 22 at 5:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

Actually your function works as you described.

I copied your function into a page template on my site without modifying it at all. I tested it with my main user, which has posted, and it said I had posted.

Then I created a dummy user that has not posted. I tested the same page and it said I had not posted. I also posted a post backdated more than 30 days, and it still said I had not posted within the last 30 days.

All of that is by design.

A couple of points to keep in mind:

  • You aren't specifying in your query which type of post is being retrieved. Are you sure that the user you are testing with hasn't posted any type of post? i.e. a custom post type, a page, a contact form via a plugin, etc.

  • Your code is also always checking the currently logged in user. Make sure that's what you want.

  • No, I would like to check for the author who created the actual post... not the logged in author. – Joanna Mikalai Feb 9 at 16:41
  • I have tried: 'author' => get_the_author_meta( 'ID' ) but this actually doesn't return TRUE, if the author posted within 30 days – Joanna Mikalai Feb 9 at 16:50
  • also I am specifying that I only want to check posts: 'post_type' => 'post', – Joanna Mikalai Feb 9 at 16:51
  • ok sorry now it works as expected by changing: 'author' => get_the_author_meta( 'ID' ) – Joanna Mikalai Feb 9 at 16:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.