繁体   English   中英

获取作者在Wordpress中的先前帖子

[英]Get previous post by author in Wordpress

我想在帖子底部显示由当前帖子作者撰写的先前帖子。

我知道我可以使用函数get_previous_post()来获取当前帖子类型的前一个帖子,但是它不能被作者过滤。

我不仅想要作者的最新帖子,还希望查看该帖子之前的帖子。

这可能有效。 $previous_post将是当前帖子作者的最后一个帖子。

<?php
$this_post = get_post();

$args = array(
    'author'        =>  $this_post->post_author,
    'post_type'     =>  $this_post->post_type,
    'orderby'       =>  'post_date',
    'order'         =>  'DESC',
    'date_query' => array(
        'before' => $this_post->post_date
    ),
);

$author_posts = get_posts( $args );
$previous_post = $author_posts[0];

我使用了WordPress 3.7中添加的date_query: http ://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

这样我们就可以将当前帖子的日期用作截止日期。 之后,获取您所需的东西相对容易。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM