如何从wp_query结果中获取发布数据数组?
-
-
直接访问发布数据与使用模板标签之间要记住的一个重要区别是,过滤器未应用于数据,某些功能可能会中断.An important difference to keep in mind between accessing post data directly versus using template tags is that filters are not applied to the data and some functionality may break.
- 2
- 2016-12-30
- Milo
-
3 个回答
- 投票数
-
- 2012-08-11
您应该阅读WordPress法典上的 WP_Query的功能参考.那里有很多示例可供参考.如果您不想使用
while
遍历结果集,则可以在属性posts <中使用
WP_Query
获取查询返回的所有帖子./code>.例如
$ query=new WP_Query(array('post_type'=&gt;'page')); $posts=$ query-&gt;posts; foreach($posts as $post){ //做你的事情,例如 //echo $post-&gt;post_name; }
You should read the function reference for WP_Query on the WordPress codex. There you have a lot of examples to look at. If you don't want to loop over the result set using a
while
, you could get all posts returned by the query with theWP_Query
in the propertyposts
.For example
$query = new WP_Query( array( 'post_type' => 'page' ) ); $posts = $query->posts; foreach($posts as $post) { // Do your stuff, e.g. // echo $post->post_name; }
-
但是,您链接到的所有示例都没有演示如何处理帖子.因此,很高兴您回答了,很遗憾他们没有在文档中提供它.另一个提示:如果您要对一个唯一的帖子进行匹配,则可以在args中使用带有类似''posts_per_page'=> 1'的函数.`function wp_queryfirstpost($ args){ $ q=新的WP_Query($ args); $pp=$ q->get_posts(); $firstpost=false;如果($pp)$firstpost=$pp [0]; wp_reset_postdata(); 返回$firstpost; }`None of the examples you link to demonstrates how to process posts, though. So it's good that you answered, pity they don't have it in the documentation. Another tip: If you're doing a match on a unique post you can use a function like this with `'posts_per_page'=>1` in args. `function wp_queryfirstpost($args) { $q=new WP_Query($args); $pp=$q->get_posts(); $firstpost=false;if ($pp) $firstpost=$pp[0]; wp_reset_postdata(); return $firstpost; }`
- 1
- 2014-03-21
- Henrik Erlandsson
-
@rofflox:你是圣人!非常适合绕开get_the_title/ID/younameit.@rofflox: You are a saint! Great for circumventing get_the_title/ID/younameit.
- 0
- 2015-04-30
- Vial
-
您应该使用$ query->posts来代替,$ query->get_posts()将触发查询解析的重新运行以及其他不必要的数据库查询You should use `$query->posts` instead, `$query->get_posts()` will trigger a re-running of the query parsing and additional unnecessary database queries
- 8
- 2015-11-01
- Tom J Nowell
-
$ query->get_posts();不能按预期工作.不知道为什么,但是返回的帖子少于查询.参见此处:https://stackoverflow.com/questions/25395299/how-do-i-get-wordpress-wp-query-get-posts-on-multiple-categories-to-work$query->get_posts(); is not working as expected. Not sure why but it returns fewer post than the query. See here: https://stackoverflow.com/questions/25395299/how-do-i-get-wordpress-wp-query-get-posts-on-multiple-categories-to-work
- 0
- 2016-11-12
- Laxmana
-
这个答案是完全错误的,当您使用一些参数创建一个新的WP_Query时,将立即在内部调用get_posts()方法,并且您不应再次调用它!如果如上例所示再次调用它,它将运行一个DIFFERENT查询,具体取决于初始运行的参数和结果(设置内部标志等),并可能返回其他(较小)的结果集或根本没有结果.正如TomJNowell和Laxmana所建议的那样,应使用$ query->posts获取帖子数据.This answer is plain wrong, when you create a new WP_Query with some arguments the method get_posts() is internally called right away and you SHOULD NOT CALL IT AGAIN! If you call it again as shown in the example above it will run a DIFFERENT query, depending on the arguments and results form the initial run (internal flags set, etc..), and can potentially return a different (smaller) set of results or no results at all. As TomJNowell and Laxmana suggested above one should use $query->posts to get the post data.
- 1
- 2016-12-04
- ivanhoe
-
- 2015-10-01
实际上,您不需要拒绝使用
while()
循环.相同的WP_Post对象已经存储在post
属性中:$query = new WP_Query( $args ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); // now $query->post is WP_Post Object, use: // $query->post->ID, $query->post->post_title, etc. } }
Actually, you don't need to refuse to use
while()
loop. Same WP_Post Object is already stored inpost
property:$query = new WP_Query( $args ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); // now $query->post is WP_Post Object, use: // $query->post->ID, $query->post->post_title, etc. } }
-
如果是多余的.`if` is redundant.
- 2
- 2017-01-26
- Akkumulator
-
不,`if`不是多余的.在这种情况下,确实是这样,但是在大多数生产情况下,您需要在if和while之间执行代码.No, `if` is not redundant. In this exact case it is, but in most production situations, you have code to execute between the if and the while.
- 2
- 2017-03-27
- magi182
-
@magi182在这种情况下,它变得多余.人们应该学习何时使用它.@magi182 Which makes it redundant, in this exact case. People should learn when to use this.
- 2
- 2017-04-03
- frodeborli
-
@frodeborli,关于以"人们应该"开头的语句的好处是,您几乎总是可以替换为"人们不会",并且该语句仍然测试为真.@frodeborli, The nice thing about statements that start with "people should" is that you can almost always substitute "people won't" and the statement still tests as true.
- 4
- 2017-04-06
- magi182
-
@magi182我可能会让一百行代码行补充上面的代码.@magi182 I could probably make a hundred nice to have code lines to complement the above code.
- 1
- 2017-04-08
- frodeborli
-
这应该是选择的答案this should be the chosen answer
- 0
- 2018-10-27
- bysanchy
-
- 2019-04-16
您还可以使用
get_posts( $args )
代替wp_Query()
,这将为您提供帖子列表you can also use
get_posts( $args )
instead ofwp_Query()
, which will give you a list of posts
当使用WP_Query方法运行查询时,我得到了一个对象.我知道可以执行循环以显示内容.但是,我的目标不是显示任何内容,而是要通过执行诸如"foreach ..."之类的操作来获取一些发布数据.我如何获取可以循环访问并获取数据的一系列帖子数据?