WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_children() and get_posts() not working (5 posts)

  1. Bilal Shahid
    Member
    Posted 1 year ago #

    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 :)

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    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?

  3. Bilal Shahid
    Member
    Posted 1 year ago #

    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.

  4. Antonio Giovanni Schiavone
    Member
    Posted 1 year ago #

    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?

  5. Bilal Shahid
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic