Make WordPress Core

Opened 11 months ago

Last modified 11 months ago

#58630 new defect (bug)

When custom taxonomy assigned to "Media" post type and multiple images assigned to that taxonomy, then count only displaying 1

Reported by: chiragrathod103's profile chiragrathod103 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.3
Component: General Keywords:
Focuses: Cc:

Description

Screenshot 1(https://prnt.sc/HoG6bAo9WqQK): as showing count shows only 1 in the admin column.
Screenshot 2(https://prnt.sc/BmGVH5LAhYwu): as shown here multiple images are assigned in "English" term under the "language" taxonomy

Change History (1)

#1 @iamchitti
11 months ago

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

Note: See TracTickets for help on using tickets.