I see galleries are represented as separate post records with post_type = 'gallery' in the database.

How is this record (gallery) associated with a basic post (type = 'post') in the terms of database records?

  • Does the gallery post type have its post_parent set? I believe that's how the attachment post type (ie, media attachments like PDFs, JPGs, etc) are "attached" to posts. (I don't have a WP database to hand, or I'd check it myself.)
    – Pat J
    Jul 18, 2013 at 2:32

2 Answers 2


WordPress galleries are implemented as shortcodes: the [gallery] shortcode should appear in the post_content of your "basic" post (whose post_type = 'post'). The shortcode's parameters specify the images to be included in the gallery by giving the post ID of the attachment-type post containing each image. So if you see two posts in wp_posts like this:

id | post_title                   | post_type  | post_parent
 1 | This post contains a gallery | post       | 0
 2 | IMG_0954                     | attachment | 1

If the first post contains the second in a gallery, its post_content will contain something like this: [gallery ids="2"]

You'll notice that the attachment post's post_parent is set to the ID of the post containing it, but this doesn't necessarily matter to the gallery: it will include whatever attachment post IDs are given in the ids="" parameter to the shortcode.

So there is no direct database relationship between a post with a gallery and that gallery's contents; it's all determined by the shortcode present in the post's content.

For a more thorough understanding of how the gallery gets generated, including the fact that you can override its output with your own using the post_gallery filter hook, see the source for the gallery_shortcode function in media.php


If you have a custom post type called 'gallery', you should be able to find it fairly easy if you can browse the database directly with phpMyAdmin. You will see your database tables on the left side of phpMyAdmin. Click on your posts tables which will probably look like "wp_posts". This will display a list of tables. Click the search icon at the top. Scroll down to the post_type field and enter "gallery". Press enter or click the "Go" button below. This should result in a list of every custom post_type in your database. Click "edit" for any table you wish to view. There you will see all of the details such as the ID for this post or the post_parent, which may be what you are looking for.

I do not have a gallery post_type, but if I want to know which post and image is related to I can search for the image within the post_type 'attachment' and even refine my search by using the specific image ID if I know it. Then I can "edit" the image and find the post parent.

By the way, you can get the ID of an image, post or anything else by going to its edit page in the admin panel and looking at the url. You will see something like "post=xxx" where xxx is the post ID.

  • I know all of this, I am coder :) I asked where is in DB connection between post and connected gallery, I see 2 record in wp_posts, one with type post 2nd one with type gellery, but I can't find where are they connected. Jul 17, 2013 at 9:25
  • Why are you expecting two different post types to be associated within the database? Can you give an example of a way that a gallery post and a regular post are used together?
    – iyrin
    Jul 17, 2013 at 9:50

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.