Function to check if author has posted within the last x days

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.";
}

Leave a Comment