[wp-trac] [WordPress Trac] #58630: When custom taxonomy assigned to "Media" post type and multiple images assigned to that taxonomy, then count only displaying 1
WordPress Trac
noreply at wordpress.org
Tue Jul 4 11:49:31 UTC 2023
#58630: When custom taxonomy assigned to "Media" post type and multiple images
assigned to that taxonomy, then count only displaying 1
-----------------------------+------------------------------
Reporter: chiragrathod103 | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version: trunk
Severity: normal | Resolution:
Keywords: | Focuses:
-----------------------------+------------------------------
Comment (by iamchitti):
WordPress uses the "count" value stored in the "count" column of the
"wp_term_taxonomy" table to display the number of objects (posts/pages)
associated with a particular term. This count is calculated and updated
when terms are linked or removed from objects using the
`wp_set_object_terms` function. Internally, this function calls
`_update_post_term_count` which executes a specific query for the
"attachment" post type.
{{{
SELECT COUNT(*) FROM wp_term_relationships, wp_posts p1 WHERE p1.ID =
wp_term_relationships.object_id AND ( post_status IN ('publish') OR (
post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM
wp_posts WHERE ID = p1.post_parent ) IN ('publish') ) ) AND post_type =
'attachment' AND term_taxonomy_id = 2;
}}}
The query looks for attachments that are linked to a published object
(post/page). It includes attachments with a "post_status" of "inherit"
only if their "post_parent" is greater than 0 and the parent post/page has
a "post_status" of "publish". This ensures that only linked attachments
whose parent objects have been published are counted
By default, attachments have a "post_status" of "inherit". This means they
inherit the post status of their parent post/page. When an attachment is
uploaded and attached to a post or page, it takes on the post status of
the parent. This allows the attachment to inherit certain attributes or
permissions from its parent.
If an attachment is not linked to any object, its "post_parent" value will
be 0. In this case, even if a term is associated with the attachment, it
won't be counted because the query specifically looks for attachments
linked to objects.
In the provided screenshot, the count is 1 because the attachment named
"screenshot-docs.google.com..." is attached to the "Hello World" post, and
the term "english" is associated with the attachment.
''' To properly update the count, follow this workflow:'''
1. Upload the attachment.
2. Attach it to an object (post/page).
3. Assign the term(s) to the attachment.
4. Check if the count is updated accordingly.
''However, there seems to be a bug in the current workflow''. If the steps
of attaching the attachment to an object and assigning terms are
interchanged(Step 2 and Step 3), the count is not updated correctly. It
still shows the older count. Ideally, the count should always be updated
regardless of the order of these steps.
Regarding the specific reason for calculating the count only for
attachments linked to objects (`post_parent !== 0`), further clarification
from the WordPress development community or documentation may be required
to understand the underlying design choices and considerations.
Let me know if anything is not clear or need more details. :)
--
Ticket URL: <https://core.trac.wordpress.org/ticket/58630#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list