最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

query - How to display related posts by same publish date?

programmeradmin3浏览0评论

I need a query to show posts with same "publish date" as related posts. For Ex. if the post is published in 2012-08-02, i want to show other posts which are published in 2012-08-02 in bottom of the post. Is there any solution? Thank You.

I need a query to show posts with same "publish date" as related posts. For Ex. if the post is published in 2012-08-02, i want to show other posts which are published in 2012-08-02 in bottom of the post. Is there any solution? Thank You.

Share Improve this question asked Jan 31, 2020 at 14:57 Roshan NorouziRoshan Norouzi 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

Get the date of current post:

$date = get_the_date('Y-m-d');

Get year, month and day from the $date variable.

$exploded = explode('-', $date);
$year = $exploded[0];
$month = $exploded[1];
$day = $exploded[2];

Lastly, query the posts that has the same publish date.

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'date_query' => array(
        array(
            'year'  => $year,
            'month' => $month,
            'day'   => $day,
        ),
    ),
);
$query = new WP_Query( $args );

Add any query arguments and display the query results as you wish.

发布评论

评论列表(0)

  1. 暂无评论