too long

What I need:

  1. User uploads pictures in a gallery via WordPress Media Uploader and publishes them in a post. Let's say post_id = 1 and the post is: [gallery ids"1,2,3,...,n"]
  2. I need a function that gets all image-ids of post_id = 1 in this gallery.
  3. The images should be liked to '.../post-title/attachment/image-title'

I've tried :

$attachements = query_posts(
    array(
        'post_type' => 'attachment',  
        'post_parent' => $post->ID,   
        'posts_per_page' => -1        
         )
);

var_dump($attachements);

My output is:

array(0) { }

Am I just too stupid?

Try the following code

 $args = array( 
'post_type' => 'attachment', 
'numberposts' => -1, 
'post_status' => null, 
'post_parent' => $page->ID);

$attachments = get_posts( $args );
var_dump($attachements);

get_post_galleries() can be used to get information about each gallery in the post. Make sure to pass false after the $post_id to return just the data.

From that point you can loop through the different galleries and pull the ids from the ids key. This is actually a string so you'll need to explode() it into an array that you'll use with array_merge() to add to a total list of ids.

Since it's possible to contain duplicate ids, running array_unique() will ensure each id is listed only once.

$post_id = 298;

$image_ids = array ();

// get all the galleries in the post
if ( $galleries = get_post_galleries( $post_id, false ) ) {

    foreach ( $galleries as $gallery ) {

        // pull the ids from each gallery
        if ( ! empty ( $gallery[ 'ids' ] ) ) {

            // merge into our final list
            $image_ids = array_merge( $image_ids, explode( ',', $gallery[ 'ids' ] ) );
        }
    }
}

// make the values unique
$image_ids = array_unique( $image_ids );    

// convert the ids to urls -- $gallery[ 'src' ] already holds this info
$image_urls = array_map( "wp_get_attachment_url", $image_ids );    

// ---------------------------- //

echo '<pre>';
print_r ( $image_ids );
print_r ( $image_urls );
echo '</pre>';