Support » How-To and Troubleshooting » Get Posts from Two Different Post Parents?

Get Posts from Two Different Post Parents?

  • I need to find a way to pass the results for two different post parents. The current code I have is:

    $attachments = get_children("post_parent=281&post_type=attachment&post_mime_type=image");

    I need $attachments to equal the query above but also a similar query with a post_parent of 190. I’ve found that I can’t put more than one post_parent into the get_children function and so far, I’ve been unable to figure out how to combine the results of two get_children calls into one.

    I’m far from a php expert so any help would be soooo appreciated!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Something like this should work (not tested):

    function my_get_children($children_ids, $ptype = 'attachment', $pmime = 'image/%') {
      global $wpdb;
      $ids = join('|', (array) $children_ids);
      $q = "SELECT * FROM {$wpdb->posts} WHERE post_type = '{$ptype}' AND post_mime_type LIKE '{$pmime}' AND post_parent REGEXP '^({$ids})$'";
      return $wpdb->get_results($wpdb->prepare($q));

    Hi caugb, thanks so much for your help!

    How do I then implement this function in the context of the equation I need:
    $attachments = my_get_children(...
    when I need the attachments from post_parents 281 and 190?


    $attachments = my_get_children(array(281, 190));

    Thanks again, caugb, for the help!
    Unfortunately it doesn’t seem to be working. I’m not getting any results from the query.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get Posts from Two Different Post Parents?’ is closed to new replies.