I am using WordPress with Nextgen Gallery by Alex Rabe for quite sometime. Once upon a time I had a good amount galleries in my website which were used in many posts through nextgen gallery shortcode. But for quality enhancement I have deleted many posts but the galleries related to those posts remain there in file system as orphan. How to find the galleries not used in post database?

Table added by nextgen gallery:


Column          Type            Null    Default     Comments
gid             bigint(20)      No       
name            varchar(255)    No       
slug            varchar(255)    No       
path            mediumtext      Yes     NULL     
title           mediumtext      Yes     NULL     
galdesc         mediumtext      Yes     NULL     
pageid          bigint(20)      Yes     0    
previewpic      bigint(20)      Yes     0    
author          bigint(20)

wp_posts (Wordpress default table)

Column          Type        Null    Default         Comments
ID              bigint(20)  No           
post_author     bigint(20)  No      0    
post_date       datetime    No      0000-00-00 00:00:00      
post_date_gmt   datetime    No      0000-00-00 00:00:00      
post_content    longtext    No   

gid is the gallery id. for an example if the gid is 8 i use the nextgen gallery shortcode [nggallery id=8] in the post and it is saved in the post_content field of wp_posts table. i want to know the gid's which are not present in any post_content of whole wp_posts table.

2 Answers 2


Goto wp_postmeta(TABLE)-> meta_key(COLUMN)->gallery_id(value) has some value in meta_value(COLUMN) against your post_id(COLUMN in this table)


If your galleries are attachment post types(?), then you could simply modify my "Internal Link Checker Plugin" to search only for galleries.

You need to change:

WHERE post_content
LIKE '%{$post->post_title}%' OR '%{$post->post_name}%' 
ORDER BY post_date


WHERE post_type = 'attachment'

This way you list the attached post types 'attachment' for the currently displayed post.

The shortcode has nothing to do with how the Media is stored in your database. It just cares about the display of the gallery.


The following lines should give you all posts that have nggallery in the post content.

WHERE post_content
LIKE '%nggallery%' 
ORDER BY post_date

After finding and removing all those posts you can savely delete that table (I wouldn't do it before).

  • As I have said I created the albums manually in nextgen gallery then used shortcodes [nggallery id=x] where x is gallery id in posts. Now some posts are deleted but not those galleries. I want to find these orphan nextgen galleries.
    – coolsaint
    Sep 2, 2011 at 18:24
  • The shortcode says nothing about the post type. They have to be saved somehow in your DB. As I have said: Use the link checker and rebuild it to search and find your DB entries.
    – kaiser
    Sep 3, 2011 at 21:48
  • thanks for your suggestion but I think there are some misunderstandings. No my galleries are not attachment post types. The galleries are created using nextgen gallery plugin and not attached. I have installed Internal Link checker plugin. But you didn't said what to modify. I have checked my DB and found out the short codes are saved as [nggallery id=x] format along with other contents in wp_post table. I want to find those entries in one place.
    – coolsaint
    Sep 4, 2011 at 19:13
  • Mate, attachments - like images - are post types of "type" attachment. You can simply remove the shortcodes from your post, but that doesn't solve the main problem: The attachments in your database. Got me now? So, if you uploaded the images for your galleries with the media uploader, then you have it right inside your database as "posts". See update.
    – kaiser
    Sep 4, 2011 at 20:48
  • i got it, attachments. But after the modifications i can see the list of all images i have uploaded by wordpress default upload option not by nextgen gallery in the post edit page. I want to print a list of nextgen galleries which are not used in any posts :(
    – coolsaint
    Sep 4, 2011 at 22:26

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.