1

Is is possible to check whether an author has posted in the last X days?

I am currently using this function to determine whether he has posted at all.

$args = array(
    'post_type'  => 'your_custom_post_type',
    'author'     => get_current_user_id(),
);

$wp_posts = get_posts($args);

if (count($wp_posts)) {
    echo "Yes, the current user has 'your_custom_post_type' posts published!";
} else {
    echo "No, the current user does not have 'your_custom_post_type' posts published.";
}
1

Yes, it is possible and it is pretty small change in your code...

get_posts() uses WP_Query, so you can use all params from this list: https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

So here's your code after changes:

$args = array(
    'post_type'  => 'your_custom_post_type',
    'author'     => get_current_user_id(),
    'date_query' => array(
        array(
            'after'     => '- X days', // <- change X to number of days
            'inclusive' => true,
        ),
     ),
     'fields' => 'ids', // you only want the count of posts, so it will be much nicer to get only IDs and not all contents from DB
);

$wp_posts = get_posts($args);

if (count($wp_posts)) {
    echo "Yes, the current user has 'your_custom_post_type' posts published!";
} else {
    echo "No, the current user does not have 'your_custom_post_type' posts published.";
}
  • hmm.. I am not getting the expected results. I just changed X to 30 and 'post_type' => 'post' – Joanna Mikalai Feb 5 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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