用于检查作者是否在最近x天内发帖的函数

时间:2019-02-04 作者:JoaMika

是否可以检查作者在过去X天内是否发表过文章?

我目前正在使用此功能来确定他是否发布了所有内容。

$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 个回复
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成

是的,这是可能的,而且代码中的更改非常小。。。

get_posts() 使用WP_Query, 因此,您可以使用此列表中的所有参数:https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

下面是更改后的代码:

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

相关推荐

如何为class-wp-query.php追踪这些PHP通知和警告的原因?

我的WordPress调试。日志文件中充满了这些PHP通知,间隔大约15分钟,然后是几个小时…[23-Mar-2018 05:33:00 UTC] PHP Notice: Trying to get property of non-object in /home/mysite/public_html/wp-includes/class-wp-query.php on line 3736 [23-Mar-2018 05:33:00 UTC] PHP Notice: Trying to get p