get_posts uniquement les enfants de certains parents
3 réponses
- votes
-
- 2011-11-22
Vous avez deux options:
-
Appelezget_postsplusieursfois: unpour lespagesparentsen utilisant
post__in => array(2,4)
,et deuxpour lespagesenfants de chaqueparent avecpost_parent => 2
etpost_parent => 4
et enfinfusionnertous les résultatsen un seultableau. -
Écrivez directement votre requête SQLet utilisez
$wpdb->get_results
.Voir cet article pour unexemple.Dans votre cas,ce serait quelque chose de similaire au code suivant (nontesté):$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);
-
lepremierestmauvais,carje ne connaispas lenombreexact d'identifiants.Le secondme semblebon.J'essaierai.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 }
Bien que ce soitbeaucoupplusfacile/plus rapide si vouspouviez les étiqueter dans unetaxonomiepersonnalisée ou lesidentifier d'une autremanière afin que vousn'ayezbesoin que de rechercher une chose,plutôt quetrois.
parexemple sinous avions unetaxonomiepersonnalisée "surlignés_produits"nouspourrionsfaire:
$children = get_posts('post_type' => 'products', 'orderby' => 'date','order' => 'DESC','highlighted_products' => 'example_page'); foreach($children as $child){ // etc }
Ce qui seraitbeaucoupplusflexible,moins sujet auxerreurs (les ID 2et 4pourraient changer! Nepas coderen dur),et c'estbeaucoupplus rapide àfaire,pas de SQLbrutni de requêtesmultiples. Sans oublier que vous avezmaintenant uneinterface utilisateur conviviale dans lebackend où vous venez detaguer un article sur lesproduitset il apparaît aubonendroit
$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; }
vouspouvez utiliser d'autres attributs (parexemple
$child->post_content
) ... si vous avezbesoin de définirpost_type,ajoutez également cet argument:&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
J'ai desmessagesparents (type demessagepersonnalisé hierarch=true) avec les ID 1,2,3,4.
Les articlesparents avec les ID 2et 4 ont despagesenfants.
Jen'aibesoin de récupérer que les articles 2et 4et toutes leurspagesenfants.
Quelque chose comme ça
Commentpuis-jemodifier cecipour renvoyer lespagesenfants desidentifiantsinclus?