1

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?

kenwebart
  • 51
  • 2
  • 9

3 Answers3

1

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>';
jgraup
  • 1,271
  • 12
  • 14
0

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);
Desh
  • 175
  • 2
  • 10
  • Output is `NULL`. :-( – kenwebart Sep 17 '13 at 09:31
  • when user uploads an image, you need to attach that image to the post you want. This code will work for images attached to a post. – Desh Sep 17 '13 at 09:35
  • this is the plugin to attach images to the post http://wordpress.org/plugins/unattach/ – Desh Sep 17 '13 at 09:40
  • Thank you very much for your help, but it was not exactly what I needed. I replaced the WordPress [gallery]-shortcode with my own function. https://gist.github.com/3xken/6593568 – kenwebart Sep 17 '13 at 12:23
  • 2
    And here is an other (better) solution: http://stackoverflow.com/questions/14277794/wordpress-3-5-own-gallery-with-included-images-doesnt-work – kenwebart Sep 22 '13 at 11:17
-1

Have you tried with get_post_galleries?

http://codex.wordpress.org/Function_Reference/get_post_galleries

ItKnol
  • 1