如何限制WP_Query获得的帖子数量?
-
-
只是''posts_per_page=5'`Just `'posts_per_page=5'`
- 0
- 2015-03-18
- Pieter Goosen
-
我用那个,但是找到了所有帖子.如果访问`found_posts`属性,则该数字大于5.我希望查询仅保留5个帖子.可能吗?@PieterGoosenI use that, but that found all the posts. If I access to the `found_posts` property, it says a higher number than 5. I want my query to hold only 5 posts. ¿Is it possible? @PieterGoosen
- 0
- 2015-03-18
- EliasNS
-
您不应该设置`nopaging`参数,将其设置为true意味着获得** all **帖子You should not set the `nopaging` parameter, setting that to true means to get **all** posts
- 0
- 2015-03-18
- Pieter Goosen
-
@PieterGoosen如果我不设置`nopaging`参数,它将得到默认值`false`,因此首页显示5个帖子,但查询包含更多内容.我向问题添加图片.@PieterGoosen If I don't set the `nopaging` parameter it gets the default that is `false`, so the frontpage shows 5 posts, but the query holds more. I add an image to the question.
- 0
- 2015-03-18
- EliasNS
-
您的评论令人困惑,您要求将页面上显示的帖子数量限制为5,即您得到的.现在,您说(请重读以前的评论:-))该查询包含更多内容.请解释.您不能设置posts_per_page,然后在同一查询中将no_paging设置为true,要么是posts_per_page ** OR **,要么将nopaging设置为trueYour comments are confusing, you asked to limit the amount of posts shown on a page to 5, that is what you get. Now, you say (reread your previous comment :-)) the query holds more. Please explain. You cannot set posts_per_page and then use no_paging set to true in the same query, it is either posts_per_page **OR** nopaging set to true
- 0
- 2015-03-18
- Pieter Goosen
-
我的问题是"获取".我认为,如果查询包含的帖子多于显示的帖子,则它的工作量超出了需要.我只想知道是否有可能避免这种情况.我不希望通过隐藏的导航获得可导航的结果.My question says "gets". I think that if the query holds more posts that the shown ones, it is doing more work than needed. I just want to know if it is possible to avoid that. I don't want navigable results with a hidden navigation.
- 0
- 2015-03-18
- EliasNS
-
该查询将不包含您要求的更多帖子.如果您要求5,如果符合要求的帖子超过5,则将检索5个帖子.对查询执行var_dump(),就像查询变量为$ query一样,执行var_dump($ query->posts).您只会看到查询的5个帖子The query will not hold more posts that you have asked for. If you ask for 5, 5 posts will be retrieved if there is more than 5 posts that matches the requirements. Do a `var_dump()` of your query, like if your query variable is `$query`, do `var_dump( $query->posts )`. You will only see the 5 posts you queried for
- 0
- 2015-03-18
- Pieter Goosen
-
5 个回答
- 投票数
-
- 2015-03-18
我认为现在我明白了您要做什么.当您使用
WP_Query
运行自定义查询并将限制设置为每页仅获得5个帖子时,该查询将仅检索5个帖子,而该查询仅包含5个帖子, BUT 为了便于分页,WP_Query
仍然遍历整个数据库,并计算与查询条件匹配的所有帖子.当您查看查询的
$found_posts
和$max_num_pages
属性时,可以看到这一点.让我们举个例子:您有20个帖子属于默认帖子类型
post
.您只需即可获得最新的5条帖子,无需分页.您的查询看起来像这样$q = new WP_Query( 'posts_per_page=5' );
-
var_dump( $q->posts )
将为您提供最新的5条帖子 -
echo $q->found_posts
将为您提供20
-
echo $q->max_num_pages
将为您提供4
在只有几篇文章的网站上,这项额外工作的影响微乎其微,但是如果您正在运行一个拥有数百或数千篇文章的网站,这可能会非常昂贵.如果您只需要5个最新职位,这会浪费资源
有一个未记录的参数,称为
no_found_rows
,该参数使用布尔值,可以在找到所需的5个帖子后使用它来使您的查询保释.这将强制WP_Query
在检索到查询的帖子数量后不再寻找符合条件的帖子.此参数已经内置在get_posts
中,这就是为什么get_posts
比WP_Query
快一点的原因,尽管get_posts
使用WP_Query
结论
最后,如果您不打算对查询使用分页,明智的做法是在查询中
'no_found_rows=true'
以节省浪费的资源.I think that now I understand what you are trying to do. When you run a custom query with
WP_Query
and set the limit to get only 5 posts per page, only 5 posts will be retrieved by the query and that query will only hold 5 posts, BUT for the sake of pagination,WP_Query
still runs through the whole database and counts all the posts that matches the criteria of the query.That can be seen when you look at the
$found_posts
and$max_num_pages
properties of the query. Lets take an example:You have 20 posts belonging to the default post type
post
. You only need the latest 5 posts without pagination. Your query looks like this$q = new WP_Query( 'posts_per_page=5' );
var_dump( $q->posts )
will give you the latest 5 posts as expectedecho $q->found_posts
will give you20
echo $q->max_num_pages
will give you4
The impact of this extra work is minimal on sites with only a few posts, but this can gt expensive if you are running a site with hundreds or thousands of posts. This is a waste of resources if you are only ever going to need the 5 latest posts
There is an undocumented parameter called
no_found_rows
which uses boolean values which you can use to make your query bail after it found the 5 posts you need. This will forceWP_Query
not to look for any more posts mathing the criteria after it has retrieved the amount of posts queried. This parameter is already build intoget_posts
, that is whyget_posts
is a bit faster thanWP_Query
althoughget_posts
usesWP_Query
Conclusion
In conclusion, if you are not going to use pagination on a query, it is always wise to
'no_found_rows=true'
in your query to speed things up and to save on wasting resources. -
- 2015-03-18
与@Pieter Goosen讨论了问题的评论之后,我认为我可以回答问题并解释我的错误.
关键是
found_posts
让我感到困惑.我认为,该数字是检索到的帖子,但不是.这是符合条件的帖子数.就像WP_Query
有两部分一样:一部分用于查找(所有)帖子,另一部分用于在检查pagination
参数时获取内容.因此,我们有一个$post_count
属性,它是获取的帖子数(Codex说The number of posts being displayed
),当然等于posts_per_page
参数,以及$posts
数组属性上的项目数.所以
WP_Query
并没有做任何无用的工作,正如我认为的^^希望这对其他人有帮助!
After the conversation with @Pieter Goosen on the comments of the question, I think I can answer the question and explain my mistake.
The key is that
found_posts
was confussing me. I thougth that, that number is the posts retrieved but is not. It is the number of posts that match the criteria. It's like theWP_Query
had 2 parts: one for finding (all) the posts, and other for fetching the content, when it checks for thepagination
parameters. So we have the$post_count
property that is the number of posts fetched (Codex saysThe number of posts being displayed
), that of course is equal to the number onposts_per_page
parameter, and the number of items on the$posts
array property.So
WP_Query
is not doing any useless work, as I thought ^^Hope this helps others!
-
看我的答案.我想我明白你的意思:-)See my answer. I think I understand what you mean :-)
- 0
- 2015-03-18
- Pieter Goosen
-
是!您做得非常好:D终于我有了办法,并且我理解了所有=D谢谢@PieterGoosen!Yes! You did it very well :D Finally I got the way to do it, and I understand all =D Thanks @PieterGoosen!
- 0
- 2015-03-19
- EliasNS
-
做完了!它扩展了我自己的答案^^ @PieterGoosenDone! It extended my own answer ^^ @PieterGoosen
- 0
- 2015-03-19
- EliasNS
-
- 2015-03-18
好,让您拥有名为"blog_posts"的帖子类型,并且您希望获取5个该帖子类型的帖子.这是您需要做的
$args = array( 'post_type' => 'blog_posts', 'posts_per_page' => '5', ); $query = new WP_Query($args);
上面的查询将返回5个类型为'blog_posts'的帖子,如果它不是自定义帖子类型,则只需替换
'post_type' => 'posts',
,如果您想获取所有帖子,请像这样'posts_per_page' => '-1',
,以获取更多详细信息 WP查询Ok , lets you have post type called 'blog_posts' , and you want to fetch 5 posts of that post type . Here is what you need to do
$args = array( 'post_type' => 'blog_posts', 'posts_per_page' => '5', ); $query = new WP_Query($args);
The above query will return 5 posts of type 'blog_posts' , if it is not a custom post type , then just replace like this
'post_type' => 'posts',
if you want to fetch all posts then replace like this'posts_per_page' => '-1',
, for more details WP Query-
请查看有关该问题的评论.See the comments on the question, please.
- 0
- 2015-03-18
- EliasNS
-
- 2015-03-18
我知道@ user1750063已经提到了代码,但是请尝试
$args = array ( 'post_type' => 'custom_post', 'nopaging' => false, 'posts_per_page' => '5', 'order' => 'DESC', 'orderby' => 'ID', ); $query = new WP_Query( $args ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); // display content } } else { // display when no posts found } wp_reset_postdata(); // Restore original Post Data
I know that @user1750063 has mentioned the code but try this
$args = array ( 'post_type' => 'custom_post', 'nopaging' => false, 'posts_per_page' => '5', 'order' => 'DESC', 'orderby' => 'ID', ); $query = new WP_Query( $args ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); // display content } } else { // display when no posts found } wp_reset_postdata(); // Restore original Post Data
-
id是无效的orderby值,而分页是无效的参数`id` is invalid as an `orderby` value and `pagination` is an invalid parameter
- 0
- 2015-03-18
- Pieter Goosen
-
分页不是有效参数.您的意思是''nopaging'=>true'?如果是,那么我将获得所有帖子.那不是我想要的@PieterGoosen我认为他的意思是" ID".`pagination`is not a valid parameter. You mean `'nopaging' => true`? If yes, then I'll get ALL posts. That's not what I want. @PieterGoosen I think he means `ID`.
- 0
- 2015-03-18
- EliasNS
-
orderby用于显示订单,对不对?它不会损害nopaging值/参数. @PieterGoosen为什么ID和orderby无效?您能否阐明这一点?orderby is for displaying the order, right? It does not harm the nopaging value/ parameter. @PieterGoosen why is ID & orderby is invalid? Can you clarify the point?
- 0
- 2015-03-18
- Shreyo Gi
-
应该是" ID",而不是"id"It should be `ID`, not `id`
- 0
- 2015-03-18
- Pieter Goosen
-
- 2020-06-18
我将使用自定义字段进行限制,请在下面查看此查询示例:
$wp_query = new WP_Query("orderby=DESC&post_type=portfolio&meta_key=FeaturedProject&meta_value=1&posts_per_page=6");
它将返回6个特色项目.
I would limit it with custom fields, check this query sample below:
$wp_query = new WP_Query("orderby=DESC&post_type=portfolio&meta_key=FeaturedProject&meta_value=1&posts_per_page=6");
It will return 6 Featured projects.
我一直在研究Google和WPSE,我反复看到的唯一一件事就是使用了
showposts
(已弃用).我熟悉
WP_Query
,并且我认为如果将posts_per_page
设置为我的限制(即5),并且将nopaging
设置为true
,它将变成类似" 好,我只给您5个帖子"之类的内容.但这不起作用.我该怎么做?