$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Function to check if author has posted within the last x days|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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
Add a comment  | 

1 Answer 1

Reset to default 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/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.";
}
Post a comment

comment list (0)

  1. No comments so far