How can i count images form NextGen Gallery, the NextGen Gallery is insite a custom field.

Custom field for NextGen Gallery is " poze "

I search for it, i found this

NextGen - Display Image Count Per Gallery

global $wpdb; 
$images = intval( $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->nggpictures WHERE galleryid = {$gallery->ID}") ); ?> 
<?php echo $images; ?>

This code it display me the 0 count of pictures...

Mode 2 wath i try

        global $wpdb; 
        $images = intval( $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->nggpictures") ); ?> 
        <?php echo $images; ?>

This code it display me the all pictures of galery's...

Mode 3 wath i try

<?php $related_posts = get_field('poze');$count = 0;if( !empty($related_posts) ){$count = count( $related_posts ); }echo $count; ?>

This code is retriving me the number of galerry's

How can i retrive the number pof pictures atached to that gallery... Ex: 12 pictures

How can i add the custom field (poze - where ID is stored) ... anny solution woud be apreciate.

  • Did you try to run the query directly in PHPMyAdmin? I suppose $gallery->ID is correct, right?
    – brasofilo
    Commented Jan 24, 2013 at 14:31
  • hy bransofilo, i am not so good with php, i found that in search.. "SELECT COUNT(*) FROM $wpdb->nggpictures" it count me all the pictures from all the gallery's. I want to integgrate somhow the Custom Field ... the custom field is the ID of the Gallery
    – Batman
    Commented Jan 24, 2013 at 17:16

1 Answer 1


Assuming you actually have something in the $gallery object (hint: confirm that by print_r($gallery) in your template), then your code should work. Perhaps step back and leave the database stuff to NextGEN, just count the image IDs:

global $nggdb;
$poze = get_field('poze');
if ($poze && count($poze)) {
    $galleryID = $poze[0]['ngg_id'];
    $imageIDs = $nggdb->get_ids_from_gallery($galleryID);
    $count = count($imageIDs);
  • Note i use custom field to add galley Custom field : poze I tried this : <?php global $wpdb; $gallery = get_post_meta($post->ID, 'poze', true); $images = intval( $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->nggpictures WHERE galleryid = {$gallery->ID}") ); ?> <?php echo ($images); ?> when i print_r it retrive me the code UP it retrive me this .. 0Array ( [0] => Array ( [ngg_id] => 14 [ngg_form] => gallery ) ) 1) when i use that code
    – Batman
    Commented Jan 25, 2013 at 0:18
  • What does get_field('poze'); return in your code? Have you confirmed that it's a gallery ID?
    – webaware
    Commented Jan 25, 2013 at 0:35
  • <?php echo get_field('poze'); ?> it retrive me the word Array [IMG]i49.tinypic.com/2vjy3rp.jpg Left - The custom Field, Right - Were I select the Galley( EX: Fire with Fire
    – Batman
    Commented Jan 25, 2013 at 0:44
  • Right, you need to get the gallery ID from within that array. print_r(get_field('poze')); to see what it has in it.
    – webaware
    Commented Jan 25, 2013 at 0:47
  • retrive me this Array ( [0] => Array ( [ngg_id] => 24 [ngg_form] => gallery ) ) ) 24 is the id of gallery
    – Batman
    Commented Jan 25, 2013 at 0:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.