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?
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.Sign up to join this community
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:
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.