How to find the WordPress Nextgen galleries which are not used in WordPress posts?

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:

wp_ngg_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.

Topic plugin-nextgen-gallery wp-query plugins Wordpress

Category Web


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

to:

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.


Edit:

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).

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.