Get Posts from Two Different Post Parents? (5 posts)

  1. sdhunt
    Posted 4 years ago #

    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!

  2. caugb
    Posted 4 years ago #

    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));
  3. sdhunt
    Posted 4 years ago #

    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?


  4. caugb
    Posted 4 years ago #

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

  5. sdhunt
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic