get_postsは特定の親の子のみ
3 回答
- 投票
-
- 2011-11-22
2つのオプションがあります:
-
get_postsを複数回呼び出します.1つは
post__in => array(2,4)
、およびpost_parent => 2
およびpost_parent => 4
そして最後に、すべての結果を1つの配列にマージします. -
SQLクエリを直接記述し、
$wpdb->get_results
を使用します.この記事<を参照してください./a>例.あなたの場合、それは次の(テストされていない)コードのようなものになります: $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);
-
IDの正確な数がわからないため、最初のオンは悪いです.2番目は私には良さそうです.私が試してみます.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 }
ただし、カスタム分類法でタグを付けたり、他の方法で識別したりして、3つではなく、1つだけを探す必要がある場合は、はるかに簡単/高速になります.
例:カスタム分類法「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や複数のクエリがなくてもはるかに高速です.言うまでもなく、バックエンドには、商品の投稿にタグを付けるだけで適切な場所に表示される、優れたユーザーフレンドリーなUIがあります.
$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)があります.
ID2および4の親投稿には子ページがあります.
投稿2と4、およびそれらのすべての子ページのみを取得する必要があります.
このようなもの
これを編集して、含まれているIDの子ページを返すにはどうすればよいですか?