php - get_posts 不早于 X 天 - Wordpress

标签 php arrays wordpress posts

在我的 Wordpress 站点中,我使用这个 get_posts代码:

get_posts(
        array (
            'numberposts' => 5,
            'orderby'=>'comment_count',
            'order'=>'DESC',
            'post_type'   => array ( 'post' )
        )

如何过滤它以使帖子不超过 10 天?所以它应该只列出过去 10 天的帖子。

最佳答案

从 3.7 开始,您可以使用 date_query https://developer.wordpress.org/reference/classes/wp_query/#date-parameters

所以它看起来像:

$args = array(
    'posts_per_page' => 5,
    'post_type' => 'post',
    'orderby' => 'comment_count',
    'order' => 'DESC',
    'date_query' => array(
        'after' => date('Y-m-d', strtotime('-10 days')) 
    )
); 
$posts = get_posts($args);

关于php - get_posts 不早于 X 天 - Wordpress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16994364/

相关文章:

php - 如何在 PHP 中检查变量是字母还是数字?

php - 从 mysql 更新到 PDO 的最佳实践

php - 混淆何时使用 addslashes 以及何时使用 htmlentities

php - 动态调用 PHP 类方法

javascript - 如何向 SSJS 数组添加元素?

c - 在数组中查找字符串 - C 编程

arrays - Matlab - 从矩阵中删除包含0的行和列

php - WooCommerce 单一添加到购物车页面中的产品动态定价

wordpress - WP API 评论 : posting a comment containing emojis?

php - 仅在单个页面上显示 A div