admin管理员组文章数量:1130349
I'm not sure whether it's a bug or i'm doing something wrong, but it doesn't seem like posts_per_page works at all when using category__in.
My query is below, even though I've set posts_per_page to 1, it's still showing all posts.
$posts = new WP_Query(array(
'post_type' => 'post',
'category__in' => wp_get_post_categories($post->ID),
'posts_per_page' => 1,
'post__not_in' => array($post->ID)
));
Any ideas?
I'm not sure whether it's a bug or i'm doing something wrong, but it doesn't seem like posts_per_page works at all when using category__in.
My query is below, even though I've set posts_per_page to 1, it's still showing all posts.
$posts = new WP_Query(array(
'post_type' => 'post',
'category__in' => wp_get_post_categories($post->ID),
'posts_per_page' => 1,
'post__not_in' => array($post->ID)
));
Any ideas?
Share Improve this question edited Nov 29, 2018 at 9:12 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Nov 29, 2018 at 8:37 KeironLoweKeironLowe 1335 bronze badges 2- Wordpress has a setting for this, found in the admin area under SETTINGS -> READING -> Blog pages show at most You can use this instead of custom-modifying your queries. It may make it a little easier to maintain your project down the road. – vikrant zilpe Commented Nov 29, 2018 at 9:11
- @vikrantzilpe It's a custom query, which is getting only one post - I'm pretty sure that setting global post count isn't a solution in this case... – Krzysiek Dróżdż Commented Nov 29, 2018 at 9:13
1 Answer
Reset to default 1i am testing your code on my dev site and it returns only 1 result (although there are 3 items in the same category), so your code seems to be fine, maybe there is some other filter applied which ignores the posts_per_page param.
You can try using suppress_filters => true param in your WP_Query args list or use the get_posts() function instead of WP_Query as the function has supress_filters enabled by default so the code would be
$posts = get_posts(array(
'post_type' => 'post',
'category__in' => wp_get_post_categories($post->ID),
'posts_per_page' => 1,
'post__not_in' => array($post->ID)
));
Hope this helps.
I'm not sure whether it's a bug or i'm doing something wrong, but it doesn't seem like posts_per_page works at all when using category__in.
My query is below, even though I've set posts_per_page to 1, it's still showing all posts.
$posts = new WP_Query(array(
'post_type' => 'post',
'category__in' => wp_get_post_categories($post->ID),
'posts_per_page' => 1,
'post__not_in' => array($post->ID)
));
Any ideas?
I'm not sure whether it's a bug or i'm doing something wrong, but it doesn't seem like posts_per_page works at all when using category__in.
My query is below, even though I've set posts_per_page to 1, it's still showing all posts.
$posts = new WP_Query(array(
'post_type' => 'post',
'category__in' => wp_get_post_categories($post->ID),
'posts_per_page' => 1,
'post__not_in' => array($post->ID)
));
Any ideas?
Share Improve this question edited Nov 29, 2018 at 9:12 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Nov 29, 2018 at 8:37 KeironLoweKeironLowe 1335 bronze badges 2- Wordpress has a setting for this, found in the admin area under SETTINGS -> READING -> Blog pages show at most You can use this instead of custom-modifying your queries. It may make it a little easier to maintain your project down the road. – vikrant zilpe Commented Nov 29, 2018 at 9:11
- @vikrantzilpe It's a custom query, which is getting only one post - I'm pretty sure that setting global post count isn't a solution in this case... – Krzysiek Dróżdż Commented Nov 29, 2018 at 9:13
1 Answer
Reset to default 1i am testing your code on my dev site and it returns only 1 result (although there are 3 items in the same category), so your code seems to be fine, maybe there is some other filter applied which ignores the posts_per_page param.
You can try using suppress_filters => true param in your WP_Query args list or use the get_posts() function instead of WP_Query as the function has supress_filters enabled by default so the code would be
$posts = get_posts(array(
'post_type' => 'post',
'category__in' => wp_get_post_categories($post->ID),
'posts_per_page' => 1,
'post__not_in' => array($post->ID)
));
Hope this helps.
本文标签: WP QueryPosts Per Page not working in combination with categoryin
版权声明:本文标题:WP Query - Posts Per Page not working in combination with category__in 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749143088a2322538.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论