admin管理员组文章数量:1317114
I'm trying to limit get_next_post
and get_previous_post
to the same author with a filter. I'm using it in a loop, as I need to display the previous and next post for each post I'm looping through.
This is what I have tried so far in my functions.php file:
// PREVIOUS AND NEXT POST FROM SAME AUTHOR
add_filter( "get_next_post_where", function($where, $in_same_term, $excluded_terms, $taxonomy, $post){
$where .= " AND p.post_author='".$post->post_author."'";
return $where;
}, 10, 5);
add_filter( "get_previous_post_where", function($where, $in_same_term, $excluded_terms, $taxonomy, $post){
$where .= " AND p.post_author='".$post->post_author."'";
return $where;
}, 10, 5);
In the actual template, I use this loop:
$story_loop = new WP_Query( array(
'post_type' => 'stories',
'posts_per_page' => -1
)
);
while ( $story_loop->have_posts() ) : $story_loop->the_post();
$next_post = get_next_post();
$prev_post = get_previous_post();
endwhile; wp_reset_query();
However, $next_post and $prev_post are not limited to the same author.
I'd prefer to avoid making a new loop just to get the next post by the same author for each post, so filtering get_next_post would be ideal.
I'm trying to limit get_next_post
and get_previous_post
to the same author with a filter. I'm using it in a loop, as I need to display the previous and next post for each post I'm looping through.
This is what I have tried so far in my functions.php file:
// PREVIOUS AND NEXT POST FROM SAME AUTHOR
add_filter( "get_next_post_where", function($where, $in_same_term, $excluded_terms, $taxonomy, $post){
$where .= " AND p.post_author='".$post->post_author."'";
return $where;
}, 10, 5);
add_filter( "get_previous_post_where", function($where, $in_same_term, $excluded_terms, $taxonomy, $post){
$where .= " AND p.post_author='".$post->post_author."'";
return $where;
}, 10, 5);
In the actual template, I use this loop:
$story_loop = new WP_Query( array(
'post_type' => 'stories',
'posts_per_page' => -1
)
);
while ( $story_loop->have_posts() ) : $story_loop->the_post();
$next_post = get_next_post();
$prev_post = get_previous_post();
endwhile; wp_reset_query();
However, $next_post and $prev_post are not limited to the same author.
I'd prefer to avoid making a new loop just to get the next post by the same author for each post, so filtering get_next_post would be ideal.
Share Improve this question edited Oct 31, 2020 at 19:51 lastnoob asked Oct 31, 2020 at 19:36 lastnooblastnoob 1632 silver badges10 bronze badges1 Answer
Reset to default 0If you add to your WP_Query the author id, next_post and previous_post will automaticly take care of that.
$query = new WP_Query( array( 'author' => 123 ) );
$query = new WP_Query( array( 'author_name' => 'rami' ) );
https://developer.wordpress/reference/classes/wp_query/
Hope this helps, stay safe!
本文标签: plugin developmentLimit getnextpost to posts from the same author
版权声明:本文标题:plugin development - Limit get_next_post to posts from the same author 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742022437a2414924.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论