wordpress 如何从自定义帖子类型中随机返回最近的帖子

lvmkulzt  于 2022-12-11  发布在  WordPress
关注(0)|答案(1)|浏览(233)

如何从自定义帖子类型中随机返回最近的帖子
我代码如下:

$args2 = array(
    'post_type' => array( 'cme-education', 'post', 'media_gallery', 'learning-zone' ),
    'posts_per_page' => '1',
    'post__not_in' => $clicked_activities_array,
    //'post__in' => $args1,
    'orderby' => 'rand',
    'post_date' => 'DESC',
    'order' => 'DESC',
    'tax_query' => array(
        array(
            'taxonomy' => 'vocabulary_1',
            'field' => 'term_id',
            'terms' => array(2, 21)
        )
    )
);
icnyk63a

icnyk63a1#

我设法通过使用date_query参数来解决这个问题,它接受一个日期过滤器,并允许我使用orderby => 'rand来获得随机帖子。无论如何,对于那些可能遇到类似问题的人,我使用的代码如下。

$before_date = date('d F Y', strtotime("+2 days"));
$after_date = date('d F Y', strtotime("-1 years"));

$recently_added_activities = array(
    'post_type' => array( 'cme-education', 'post', 'media_gallery', 'learning-zone' ),
    'posts_per_page' => '1',
    'post__not_in' => $clicked_activities_array,
    'orderby' => 'rand',
    'date_query' => array(
        array(
            'after'     => $after_date,
            'before'     => $before_date,
            'inclusive' => true,
        ),
    ),
    'order' => 'DESC',
    'tax_query' => array(
        array(
            'taxonomy' => 'vocabulary_1',
            'field' => 'name',
            'terms' => $postTerm
        ),
    ),
);

相关问题