WordPress.org

Make WordPress Core

Opened 9 months ago

Last modified 7 months ago

#46350 new defect (bug)

If a term is '0' clicking on its "Count" value displays all posts

Reported by: jesin Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.1
Component: Taxonomy Keywords: has-patch needs-testing
Focuses: Cc:
PR Number:

Description

To replicate:

  • Create a post category named "0" (just the number without quotes).
  • Assign a few posts to this category.
  • Go to WP-Admin > Categories and the "Count" column in the "0" row will display the number of posts assigned to this category. Click on this number.

Now instead of displaying posts with the category "0", all posts are displayed irrespective of the category.

This issue isn't unique to post categories. It happens with custom taxonomies too.

Attachments (1)

46350.patch (1.2 KB) - added by clescuyer 7 months ago.

Download all attachments as: .zip

Change History (5)

#1 follow-up: @desrosj
9 months ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

Thanks for opening this, @jesin!

This is most likely being caused by a loose comparison or two somewhere. The first part of the problem looks to be

I tracked part of the issue to this conditional (introduced in [15860]). But, in my initial testing, there is still another loose comparison somewhere.

#2 @desrosj
9 months ago

  • Version changed from 5.1 to 3.1

#3 in reply to: ↑ 1 @jesin
9 months ago

I tracked down this issue to this array_filter function. Before being passed through this function the $query variable contains the following data:

Array
(
    [taxonomy] => category
    [terms] => Array
        (
            [0] => 0
        )

    [field] => name
    [operator] => IN
    [include_children] => 1
)

The array in the terms index is removed by array_filter().

How about replacing it with

$terms = array_filter( $query['terms'], function( $term ) { return ( 0 !== strlen( trim( $term ) ) ); } );

Also this issue occurs even when programmically retrieving posts using tax_query. The following code returns an empty array even if there are posts assigned to the "0" category.

get_posts( array( 'posts_per_page' => -1, 'fields' => 'ids', 'tax_query' => array( array( 'taxonomy' => 'category', 'field' => 'name', 'terms' => '0' ) ) ) )

#4 @clescuyer
7 months ago

  • Keywords has-patch needs-testing added; needs-patch removed

A very similar issue happens with tags. To replicate:

  • Create a post tag named "0" (just the number without quotes).
  • Add this tag to a few posts
  • Go to WP-Admin > Tags and the "Count" column in the "0" row will display the number of posts with this tag. Click on this number.

Now instead of displaying posts with the category "0", no posts are displayed.

For the categories, I used the array_filter() with the callback @jesin suggested. For the tags I suggest an enhanced test instead of ! empty( $q[ $t->query_var ] ).

Issue analyzed and solved during WordCamp Paris 2019 Contributor day.

@clescuyer
7 months ago

Note: See TracTickets for help on using tickets.