WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] get_children() and get_posts() not working

[Resolved] get_children() and get_posts() not working

  • Bilal Shahid
    Member

    @bilalvirgo10

    Hello there,
    I’m currently working on a theme that supports image, gallery, link and quote post formats. But the problem is that whenever I try to get attachments of a post that has a post-format other than standard (like image, gallery etc.) it returns an empty array.
    But when I use the same functions with standard post types, they work just fine and returns all the attachments. I have not been able to sort the problem yet.
    Here is the code that I use inside the loop:

    <?php
    $children = get_children(array(
          'post_parent' => $post->ID,
          'post_type' => 'attachment'
           ));
    print_r($children);
    ?>

    waiting for the reply,
    thanks in advance 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • keesiemeijer
    Moderator

    @keesiemeijer

    Are you sure the image is attached to the post? Check by editing the post, click “Add Media” and choose from the dropdown “Uploaded to this post”, the image should appear.

    Are you using this inside a loop, where the post ID is available?

    Bilal Shahid
    Member

    @bilalvirgo10

    Oh yeah…!! thanks man, you are great 🙂 , the image is not showing up in “uploaded to this post”.
    but I’m still wondering why the image is not being shown in the “uploaded to this post” when it’s appearing in the content as a caption image. Shouldn’t the caption image work as an attachment?
    Thanks again.

    I have a similar problem.
    I notice that if I upload an image into a post, this image is considered an “attachment” (and the above code works fine), but if i get an image from the media library and insert it into the post, this is not considered an attachment (So the above code returns nothing).

    I’d like to catch the images in both the situation (uploaded or taken from the media library). How I can do this? I still have to use get_children() or i must use another function?

    Bilal Shahid
    Member

    @bilalvirgo10

    As far as I have searched about it, you can’t “attach” an image to a post unless you upload one for it. You will have to upload an image to a post in order to attach it since insertion and attachment are two different things.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] get_children() and get_posts() not working’ is closed to new replies.