WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to make a nav to access all the different galleries on a single page.

Instead of hard-coding all their names out. I was curious if it was possible to make this dynamic and just call all the ID's of all the galleries, with maybe an exclusionary on the two that are slideshows.

Anyone got any fresh ideas?

share|improve this question
up vote 6 down vote accepted

All your Galleries are stored a table called wp_ngg_gallery in your wordpress database. (For the sake of completeness: nextGen Gallery creates 2 more tables for albums and pictures, respectively)

$gallery_ids = $wpdb->get_results(
    "SELECT gid FROM ".$wpdb->prefix."ngg_gallery ORDER BY gid ASC", ARRAY_A);

fetches the IDs of all galleries into an array, ordered by ID from 1 to end. Now for excluding the two that are slideshows (the example will assume their IDs are 4 and 25):

$gallery_ids = $wpdb->get_results(
    "SELECT gid
     FROM ".$wpdb->prefix."ngg_gallery
     WHERE gid NOT IN (4, 25)
     ORDER BY gid ASC", ARRAY_A);

What good are the IDs without the galleries' titles? Also, wouldn't it be easier to exclude by title? So this

$galleries = $wpdb->get_results(
    "SELECT gid, title
     FROM ".$wpdb->prefix."ngg_gallery
     WHERE title NOT IN (slideshow_1, slideshow_2)
     ORDER BY title ASC", ARRAY_A);

would give you an array of all non-slideshow galleries ordered alphabetically by gallery title. You could also select with a wildcard * instead of gid, title, then you'd get all columns from the gallery table, which are gid, name, slug, path, title, galdesc, pageid, previewpic, author.

The resulting array is obviously meaningless unless you do something with it, such as iterating over it and creating a list of links.

The structure of the resulting array is:

    [0] => Array
            ['gid'] => 4
            ['title'] => 'playing_football'
    [1] => Array
            ['gid'] => 8
            ['title'] => 'vacation_pics'

You get the idea.

EDIT: Creating a navigation from the above database query

Assuming that you already have created pages for all galleries from within the NextGen Gallery > Manage Gallery dialog, the following will create a simple navigation from it. The pageid and title columns must have been selected from the databse.

echo '<ul>';

foreach ( $galleries as $gallery ) {
    echo '<li><a href="' .
         get_bloginfo( 'url' ) .
         '/?p=' .
         $gallery['pageid'] .
         '">' .
         $gallery['title'] .

echo '</ul>';

Reading, in case you care:

share|improve this answer
Before you attempt to do something with the array, it is a good idea to dump it on screen and familiarizing yourself with its contents: var_dump($galleries); (var_dump is God's gift to php developers...) – Johannes Pille Nov 18 '11 at 1:07
Johannes, this is the single best answer I've ever gotten. Thank you so much for your contributions to the community. I can't even tell you how thankful I am ! – Trip Nov 21 '11 at 1:08
I was curious how I could echo this array. Or basically, how to call this object properly and display it? – Trip Nov 21 '11 at 1:58
@Trip Edited the answer to include an example for creating a simple navigation. I'm sure you can adjust it to your needs. In your code, the echo statement should probably a one-liner. I inserted line breaks after concatenation in the above only for legibility within this site. – Johannes Pille Nov 21 '11 at 2:41
You don't really need to manually query the db. That plugin has some basic API which you can use to get its data from the database. See for example find_all_galleries() in ngg-db.php – onetrickpony Nov 21 '11 at 2:51

Or more simply:


global $nggdb;
$galleries = $nggdb->find_all_galleries();

share|improve this answer

If anyone needs to add a menu of galleries using a certain album on a certain page can use my code.

<div id="gallery_menu">

            // hardcoded part
                $album_id = 2;
                $page_id = 12;
                $album_id = 3;
                $page_id = 14;
                $album_id = 4;
                $page_id = 16;
                $album_id = 2;
                $page_id = 12;

            // get the needed albums gallery id's
            $query = "SELECT sortorder FROM wp_ngg_album WHERE id=".(int)$album_id;
            $result = mysql_query($query) or die(mysql_error());
            while($row = mysql_fetch_array($result)){
                    $tmp = unserialize($row['sortorder']);

            // get gallery titles according to id's we got
            $query_titles = "SELECT title, gid FROM wp_ngg_gallery WHERE gid IN(";
            for($i=0; $i < count($tmp); $i++){
                if($i != (count($tmp)-1)){
                    $query_titles .= (int)$tmp[$i].", ";
                }elseif($i == (count($tmp)-1)){
                    $query_titles .= (int)$tmp[$i].")";

            // query and show titles with links
            $result_titles = mysql_query($query_titles) or die(mysql_error());
            echo '<ul class="gallery_titles">';
                while($row_titles = mysql_fetch_array($result_titles)){
                    echo '<li><a href="'.get_bloginfo('siteurl').'/?p='.$page_id.'&album='.$album_id.'&gallery='.$row_titles['gid'].'">'.$row_titles['title'].'</a></li>';
            echo '</ul>';

share|improve this answer
Do not use mysql_query(), use the WP API instead. – toscho Oct 10 '12 at 16:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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