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.";
}
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.";
}
Share
Improve this question
edited Feb 4, 2019 at 17:15
Krzysiek Dróżdż
25.6k9 gold badges53 silver badges74 bronze badges
asked Feb 4, 2019 at 16:34
JoaMikaJoaMika
6986 gold badges27 silver badges58 bronze badges
1 Answer
Reset to default 1Yes, 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/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.";
}