After the Woocommerce dummy products are imported, I notice that in the database, there are different records in different tables but point to THE SAME photo attachment.
The question is why? What are the differences between those records? Is that a duplication/redundancy in data storing?
post_id=16 as below snapshot. We would see as below.
wp_posts, the id=16 post has
post_type=attachment and its
wp_postmeta, the one with post_id=16 is also taking the
meta_valuepoints to the same image
The posts table contains information on the attachment post.
In WordPress every uploaded media has it’s own post entry, where
In fact, you can get attachment post types using functions used to get other post types, like post, page or any CPT.
For every attachment post
post_titlecolumn is used for attachment title
post_excerptis used for attachment caption
post_contentis used for attachment description
Note that attachment post type may have a
post_parent that points to the ID of the post where the attachment was uploaded from.
get_posts( 'post_type=attachment&post_parent=10' );
Retrieve all the post objects for attachments loaded from the edit page of the post with ID
However, there is information for media (especially images) that simply doesn’t fit the posts table.
All this information is stored in the postmeta table, where the
post_id column refers to the attachment row in the posts table.
For example, the
"alt" attribute is stored in a meta field that has the key
The meta field with key
'_wp_attached_file' contains the relative path (to content folder) of the uploaded file.
That is used to build the attachment URL and path, in functions like
get_attached_file() and so on.
Finally, the field
'_wp_attachment_metadata' contains different information (in a serialized array in the database):
In fact, every time an image is uploaded, WordPress creates different copies of the image in different sizes.
Please read this answer for more details.
"sizes" in the
'_wp_attachment_metadata' field contain an array with information on all generated images: the file created, the size (width and height) and so on.
Note that the preferred way to get information from
'_wp_attachment_metadata' is not using the common
get_post_meta() but the specific
wp_get_attachment_metadata(), as it triggers a filter.
If you are interested in the information about a specific generated image, the function
image_get_intermediate_size() will be handy.
Generally speaking, if you need information on an attachment, use the specific functions and avoid pulling data from the database; it is harder and does not triggers hooks, possibly breaking core, theme and plugins compatibility.
There are a lot of functions in WordPress to work with attachments, this should give you an idea…
However, main functions are all listed in this answer.
First is showing the attachment file path, second is file attributes size, type, height, width etc.
attachment in wp_post shows the attachment basic details title, time, author etc.
wp_postmeta shows attachment all other details for example attachment would be text, pdf, image etc so it having different meta data for different types of posts.