• Getting a child (i.e. attached image) in a published post works fine:

    $imgargs = array(
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'numberposts' => -1,
        'post_status' => 'any',
        'post_parent' => $post->ID
    );
    
    $images =& get_children( $imgargs,ARRAY_A);
    $link = array();
    foreach ( $images as $attachment_id => $attachment ) {
        $link[] = $attachment_id;
    }

    Doing the same on a draft post returns nothing. I guess this is by design (having look at wp_posts).

    Any idea/tip on how I can find the attached images to a draft post? I need for an add-in to /wp-admin/post.php?action=edit

Viewing 1 replies (of 1 total)
  • Thread Starter Per S?derlind

    (@pers)

    Almost there:

    // from wp-admin/includes/media.php:get_media_items( $post_id, $errors )
    if ( $post_id ) {
        $post = get_post($post_id);
        if ( $post && $post->post_type == 'attachment' )
            $attachments = array($post->ID => $post);
        else
            $attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
    } else {
        if ( is_array($GLOBALS['wp_the_query']->posts) )
            foreach ( $GLOBALS['wp_the_query']->posts as $attachment )
                $attachments[$attachment->ID] = $attachment;
    }
    
    $link = array();
    foreach ( (array) $attachments as $id => $attachment ) {
        if ( $attachment->post_status == 'trash' )
            continue;
        $link[] = $id;
    }

    this works for draft post if I upload and add an image to the post, but it doesn’t work if I select an existing image from the image library and add it to the post.

Viewing 1 replies (of 1 total)
  • The topic ‘get_children in draft post’ is closed to new replies.