get_posts某些父母的唯一孩子
3 个回答
- 投票数
-
- 2011-11-22
您有两种选择:
-
多次调用get_posts:一次使用
post__in => array(2,4)
,每个父页面的两个子页面带有两个post_parent => 2
和post_parent => 4
,最后将所有结果合并到一个数组中. -
直接编写您的SQL查询,并使用
$wpdb->get_results
.请参阅本文为例.在您的情况下,它将类似于以下(未经测试的)代码:$query_sql = " SELECT * FROM $wpdb->posts WHERE post_type = 'products' AND (ID IN (2,4) OR post_parent IN (2,4)) ORDER BY post_date DESC "; $query_result = $wpdb->get_results($query_sql, OBJECT);
You have two options:
Call get_posts multiple times: one for the parent pages using
post__in => array(2,4)
, and two for the child pages of each parent withpost_parent => 2
andpost_parent => 4
and finally merge all the results into a single array.Write directly your SQL query and use
$wpdb->get_results
. See this article for an example. In your case it would be something similar to the following (not tested) code:$query_sql = " SELECT * FROM $wpdb->posts WHERE post_type = 'products' AND (ID IN (2,4) OR post_parent IN (2,4)) ORDER BY post_date DESC "; $query_result = $wpdb->get_results($query_sql, OBJECT);
-
首先是不好的,因为我不知道可能有确切的Ids数量.第二个对我来说似乎很好.我会尽力.the first on is bad, becouse I dont know exact count of Ids there might be. The second on seem good to me. Ill try.
- 0
- 2011-11-22
- jam
-
- 2011-11-22
$args = array('orderby' => 'date','order' => 'DESC','post_type' => 'products', 'include' => '2, 4'); // get the 2 and 4 posts $children = get_posts($args); // get the children of 2, and merge arrays $children = array_merge($children,get_posts(array('orderby' => 'date','order' => 'DESC','post_type' => 'products','post_parent'=>2))); // get the children of 4, and merge arrays $children = array_merge($children,get_posts(array('orderby' => 'date','order' => 'DESC','post_type' => 'products','post_parent'=>4))); foreach($children as $child){ // etc }
尽管如果您可以在自定义分类法中为它们添加标签或以其他方式标识它们,这样您将只需要查找一件事,而不是三件事,将更加轻松/快捷.
例如如果我们有一个自定义的分类法" highlighted_products",我们可以这样做:
$children = get_posts('post_type' => 'products', 'orderby' => 'date','order' => 'DESC','highlighted_products' => 'example_page'); foreach($children as $child){ // etc }
这将更加灵活,更不容易出错(ID 2和4可能会更改!不要硬编码),并且这样做的速度要快得多,无需原始SQL或多个查询.更不用说您现在在后端有一个不错的用户友好用户界面,您只需在其中标记产品信息,它就会显示在正确的位置
$args = array('orderby' => 'date','order' => 'DESC','post_type' => 'products', 'include' => '2, 4'); // get the 2 and 4 posts $children = get_posts($args); // get the children of 2, and merge arrays $children = array_merge($children,get_posts(array('orderby' => 'date','order' => 'DESC','post_type' => 'products','post_parent'=>2))); // get the children of 4, and merge arrays $children = array_merge($children,get_posts(array('orderby' => 'date','order' => 'DESC','post_type' => 'products','post_parent'=>4))); foreach($children as $child){ // etc }
Although it would be much much easier/faster if you could tag them in a custom taxonomy or identify them some other way so that you were only needing to look for one thing, rather than 3 things.
e.g. if we had a custom taxonomy 'highlighted_products' we might do:
$children = get_posts('post_type' => 'products', 'orderby' => 'date','order' => 'DESC','highlighted_products' => 'example_page'); foreach($children as $child){ // etc }
Which would be far more flexible, less prone to errors ( ID 2 and 4 might change! Don't hardcode ), and it's a lot faster to do, no raw SQL or multiple queries. Not to mention that you now have a nice user friendly UI in the backend where you just tag a products post and it appears in the right place
-
- 2013-11-13
$children = get_posts('post_parent=SLUG_OF_PARENT_POST&post_status=publish'); foreach($children as $child) { echo '<br/>ID:'.$child->ID; }
您可以使用其他属性(即
$child->post_content
)... 如果您需要定义post_type,那么也要添加此参数:&post_type=POST_TYPE_NAME
$children = get_posts('post_parent=SLUG_OF_PARENT_POST&post_status=publish'); foreach($children as $child) { echo '<br/>ID:'.$child->ID; }
you can use other attributes (i.e.
$child->post_content
)... if you need to define post_type, then add this argument too :&post_type=POST_TYPE_NAME
我有ID为1、2、3、4的父帖子(自定义帖子类型hierarch=true).
ID为2和4的父帖子具有子页面.
我只需要检索帖子2和4及其所有子页面.
类似这样的东西
如何编辑此代码以返回包含ID的子页面?