posts_per_page没有限制
-
-
我已经搜寻Google一段时间了,正在搜索WP Codex,但是我找不到这个简单问题的直接答案.我相信示例代码清楚地表达了我的问题,以及我的尝试是什么(为参数设置了很高的值).我不是WP专家,所以这就是为什么我来这里问这个问题.甚至提供对您似乎微不足道的问题的答案,也有助于发展这些Stack Exchange社区.我个人很喜欢在搜索结果中看到一个Stack Overflow链接,而不是一个糟糕的论坛的链接.I've been Googling for awhile and searching the WP codex, but I just can't find a direct answer to this simple question. I believe my question is worded clearly with example code, and what my attempt was (setting a high value for the argument). I'm not a WP expert, so thats why I came here to ask the question. Even providing answers to questions that seem trivial to you are helpful in growing these Stack Exchange communities. I personally love seeing a Stack Overflow link in my search results, as opposed to a link to a crappy forum.
- 6
- 2011-12-11
- Banjer
-
另外,感谢您的回答.您应该将其发布为答案而不是评论,以便我接受.Also, thanks for the answer. You should post it as an answer and not a comment, so I can accept it.
- 0
- 2011-12-11
- Banjer
-
我已表达您的意思,也非常感谢您在"写"这个问题上所付出的努力.我也同意,非专家问题可以证明自己在这个社区中很有价值.另一方面,太多这样的问题会阻止一些专家参与其中.我想这全都是某种平衡.无论如何,我是一个大推动者,所以我期待着您的下一个问题:)在WPSE上度过愉快的时光.I've got your point and I do appreciate your effort in *writing* the question. I also agree that non-expert questions can prove themselves valuable in this community. On the other hand too many of such questions can discourage some experts from getting involved here. It's all about some kind of balance I guess. Anyway I'm a big up-voter so I'm looking forward to your next questions :) Have a great time here on WPSE.
- 0
- 2011-12-11
- Michal Mau
-
PS:您还想将`type`替换为`post_type`(或完全删除此行).我将同时编辑Rutwick的答案和您的问题,以防止有人复制粘贴此小错误.PS: you also want to replace `type` for `post_type` (or remove this line altogether). I will edit both the Rutwick's answer and your question to prevent somebody from copy-pasting this little mistake.
- 0
- 2011-12-11
- Michal Mau
-
@Maugly感谢您的纠正人...只专注于posts_per_page,因此复制了错字!;)@Maugly Thanks for the correction man... just focused on posts_per_page hence copied the typo! ;)
- 0
- 2011-12-11
- Rutwick Gangurde
-
6 个回答
- 投票数
-
- 2011-12-11
-1是您的答案!在此处中查找
posts_per_page
.$args = array( 'post_type' => 'post', 'cat' => '22,47,67', 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 1, 'depth' => 1, 'posts_per_page' => -1 );
重要警告:这可能会导致非常庞大的查询,从而使网站瘫痪.仅在确定数据库可以处理时才执行此操作.不在公共主题或插件中.
-1 is your answer! Look for
posts_per_page
here.$args = array( 'post_type' => 'post', 'cat' => '22,47,67', 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 1, 'depth' => 1, 'posts_per_page' => -1 );
Important caveat: This can result in a very huge query that can bring the site down. Do this only if you are sure your database can handle it. Not in public themes or plugins.
-
重要警告:这可能导致非常庞大的查询,从而使网站瘫痪.仅当您确定数据库可以处理它时,才这样做.不在公共主题或插件中.Important caveat: This can result in a very huge query that can bring the site down. Do this only if you are **sure** your database can handle it. Not in public themes or plugins.
- 7
- 2016-08-11
- fuxia
-
@toscho添加您的评论作为答案的更新.@toscho Adding your comment as an update to the answer.
- 0
- 2016-11-23
- Rutwick Gangurde
-
你救了我的命!you save my life!!
- 0
- 2019-10-11
- Darlan Dieterich
-
@DarlanDieterich很高兴我可以提供帮助!:)@DarlanDieterich Glad I could help! :)
- 0
- 2019-10-14
- Rutwick Gangurde
-
- 2011-12-12
或者,您也可以传递
WP_Query
(这是query_posts
使用nopaging
参数,该参数基本上具有相同的作用.$args = array( 'nopaging' => true // Your other args, etc.. );
它的作用完全相同,但是如果您以后再回头看却不记得自己在做什么,我个人觉得对您来说更清楚了,您打算使用该参数是什么在args数组中.
但是,正如我提到的那样,他们实际上都会取得相同的成就.
拥有不止一种方法很无伤大雅,分享您所知道的东西总是很高兴的,足以说这就是我回答的原因,尽管您已经有足够的方法..;)
Or alternatively you can pass
WP_Query
(which is whatquery_posts
uses) thenopaging
argument, which basically does the same thing..$args = array( 'nopaging' => true // Your other args, etc.. );
It will do exactly the same, but if you have to look back at it later and can't remember what you were doing, i personally feel it will be more clear to you, what it is you were intending with that parameter inside the args array.
As i mentioned however, they'll both actually achieve the same.
Can't hurt to have more than one approach, and it's always nice to share what you know, suffice to say that's the reason for my answer, despite you already having a sufficient one.. ;)
-
- 2014-02-23
来自您的儿童主题功能文件:
add_action( 'pre_get_posts', 'wpsites_no_limit_posts' ); function wpsites_no_limit_posts( $query ) { if( $query->is_main_query() && !is_admin() && is_home() ) { $query->set( 'posts_per_page', '-1' ); $query->set( 'order', 'ASC' ); $query->set( 'post_type', 'post' ); $query->set( 'cat', '22,47,67' ); $query->set( 'orderby', 'name' ); $query->set( 'order', 'ASC' ); $query->set( 'hide_empty', '1' ); $query->set( 'depth', '1' ); } }
From your child themes functions file:
add_action( 'pre_get_posts', 'wpsites_no_limit_posts' ); function wpsites_no_limit_posts( $query ) { if( $query->is_main_query() && !is_admin() && is_home() ) { $query->set( 'posts_per_page', '-1' ); $query->set( 'order', 'ASC' ); $query->set( 'post_type', 'post' ); $query->set( 'cat', '22,47,67' ); $query->set( 'orderby', 'name' ); $query->set( 'order', 'ASC' ); $query->set( 'hide_empty', '1' ); $query->set( 'depth', '1' ); } }
-
- 2019-10-17
使用里卡多(Ricardo)进行一些修改:
static $paged; $post_ids = []; do { $paged++; $defaults = [ 'fields' => 'ids', // Only return the ID field to make this query faster. 'post_type' => 'post', 'posts_per_page' => 100, 'no_found_rows' => false, // We need pagination & the count for all posts found. 'paged' => $paged, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, ]; $query = new WP_Query($defaults); if ($query->have_posts()) { foreach ($query->posts as $id) { $post_ids[] = $id; } } } while ($query->max_num_pages > $paged); return $post_ids;
通过仅查询ID行并避免更新术语和元缓存,这将大大增加查询时间.
Using Ricardo's with some modification:
static $paged; $post_ids = []; do { $paged++; $defaults = [ 'fields' => 'ids', // Only return the ID field to make this query faster. 'post_type' => 'post', 'posts_per_page' => 100, 'no_found_rows' => false, // We need pagination & the count for all posts found. 'paged' => $paged, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, ]; $query = new WP_Query($defaults); if ($query->have_posts()) { foreach ($query->posts as $id) { $post_ids[] = $id; } } } while ($query->max_num_pages > $paged); return $post_ids;
This will dramatically increase the query time by only querying against the ID row and avoiding updating term and meta cache.
-
真好!感谢分享.Nice! thanks for sharing.
- 0
- 2019-10-18
- Ricardo Canelas
-
- 2016-08-11
您问题的正确答案是
'posts_per_page' => -1
,因为-1
每页将返回无限制的帖子,就像其他用户回答一样.我只想在此问答中添加一个附件,
如果要从WordPress管理面板上的阅读设置中获取每页的帖子数,则必须调用
get_option()
函数并将posts_per_page
作为字符串.$args = array( 'post_type' => 'post', 'cat' => '22,47,67', 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 1, 'depth' => 1, 'posts_per_page' => get_option('posts_per_page') );
我希望这个答案能对我有所帮助.快乐的编码Stackexchange用户
The right answer for your issue is
'posts_per_page' => -1
because-1
will return unlimited posts per page As the others users answer.I just want to add an add-on to this Q/A,
If you want to get the number of posts per page from the reading setting on WordPress Administration Panel you have to call the
get_option()
function and passposts_per_page
as a string to it.$args = array( 'post_type' => 'post', 'cat' => '22,47,67', 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 1, 'depth' => 1, 'posts_per_page' => get_option('posts_per_page') );
I hope this answer will help someone as it help me. Happy Coding Stackexchange Users
-
这实际上是一个不错的补充!That's actually a nice addition!
- 0
- 2017-12-29
- Herbert Van-Vliet
-
- 2017-02-22
或..
function getAll($arg, $posts_per_page = 50) { $data = array(); $page = 1; do{ $arg['paged'] = $page; $arg['posts_per_page'] = $posts_per_page; $query = new WP_Query($arg); foreach ($query->posts as $post){ $data[] = $post; } $page++; }while(count($query->posts) === $posts_per_page); return $data; }
Or..
function getAll($arg, $posts_per_page = 50) { $data = array(); $page = 1; do{ $arg['paged'] = $page; $arg['posts_per_page'] = $posts_per_page; $query = new WP_Query($arg); foreach ($query->posts as $post){ $data[] = $post; } $page++; }while(count($query->posts) === $posts_per_page); return $data; }
-
您应该添加上下文以解释您的代码,以解决问题的想法.You should add context to explain your code, your idea to solve the question.
- 2
- 2017-02-22
- bueltge
我想返回所有带有
query_posts
的帖子.我尝试将posts_per_page
设置为一个很高的数字,但是query_posts
却吓了一跳,并且不返回任何帖子.没有限制地查询帖子的正确方法是什么?