get_posts only children from certain parents

You have two options:

  1. 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 with post_parent => 2 and post_parent => 4 and finally merge all the results into a single array.

  2. 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);
    

Leave a Comment