Make WordPress Core

Opened 6 years ago

Last modified 6 days ago

#46350 new defect (bug)

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

Reported by: jesin's profile jesin Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.1
Component: Taxonomy Keywords: has-patch has-unit-tests has-test-info
Focuses: Cc:

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 6 years ago.

Download all attachments as: .zip

Change History (10)

#1 follow-up: @desrosj
6 years 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
6 years ago

  • Version changed from 5.1 to 3.1

#3 in reply to: ↑ 1 @jesin
6 years 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
6 years 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
6 years ago

This ticket was mentioned in PR #8983 on WordPress/wordpress-develop by @SirLouen.


7 days ago
#5

  • Keywords has-unit-tests added

Some little improvement over the previous patch + Unit Tests

Trac ticket: https://core.trac.wordpress.org/ticket/46350

#6 @SirLouen
7 days ago

  • Keywords has-test-info added

Test Report

Description

🟠 This report can't fully validate that the indicated patch works as expected.

Patch tested: https://core.trac.wordpress.org/attachment/ticket/46350/46350.patch

Environment

  • WordPress: 6.9-alpha-60093-src
  • PHP: 8.2.28
  • Server: nginx/1.27.5
  • Database: mysqli (Server: 8.4.5 / Client: mysqlnd 8.2.28)
  • Browser: Chrome 137.0.0.0
  • OS: Windows 10/11
  • Theme: Twenty Twenty-Five 1.2
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.2.0

Test Instructions

  • Following the ones provided in the OP post

Actual Results

  1. ✅ Issue resolved with patch.
  2. ❌ Some units tests not passing (Ticket #20604)

Additional Notes

  • I have submitted a new patch that solves the Unit Testing problems, and adds a test to cover the issue provided in this report.
  • Need some testing for my current patch.

This ticket was mentioned in Slack in #core-test by sirlouen. View the logs.


6 days ago

#8 @ravigadhiyawp
6 days ago

Test Report

Description

This report validates whether the indicated patch works as expected.

Patch tested: https://patch-diff.githubusercontent.com/raw/WordPress/wordpress-develop/pull/8983.diff

Environment

  • WordPress: 6.9-alpha-60093-src
  • PHP: 8.2.28
  • Server: nginx/1.27.5
  • Database: mysqli (Server: 8.4.5 / Client: mysqlnd 8.2.28)
  • Browser: Chrome 137.0.0.0
  • OS: Windows 10/11
  • Theme: Twenty Twenty-Five 1.2
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.2.0

Actual Results

  1. ✅ Issue resolved with patch.

Additional Notes

  • Issue is resolved with patch
  • Also, unit tests also passing

Supplemental Artifacts

I have run the unit test, this patch also passing the unit test.

#9 @SirLouen
6 days ago

  • Keywords needs-testing removed
Note: See TracTickets for help on using tickets.