-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Latest Posts Block: Include posts from child categories when parent is selected #69793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Latest Posts Block: Include posts from child categories when parent is selected #69793
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Mamaduka
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to handle this at the REST API/WP_Query level, as suggested in this comment, instead of trying to enhance the categories list on the client side.
| let catIds = [ ...selectedCatIds ]; | ||
|
|
||
| if ( allCategories && selectedCatIds.length > 0 ) { | ||
| const childrenMap = {}; | ||
|
|
||
| allCategories.forEach( ( category ) => { | ||
| const parentId = category.parent; | ||
| if ( parentId ) { | ||
| if ( ! childrenMap[ parentId ] ) { | ||
| childrenMap[ parentId ] = []; | ||
| } | ||
| childrenMap[ parentId ].push( category.id ); | ||
| } | ||
| } ); | ||
|
|
||
| const getAllDescendants = ( parentIds ) => { | ||
| let descendants = []; | ||
| parentIds.forEach( ( parentId ) => { | ||
| const children = childrenMap[ parentId ] || []; | ||
| descendants = [ ...descendants, ...children ]; | ||
| if ( children.length > 0 ) { | ||
| descendants = [ | ||
| ...descendants, | ||
| ...getAllDescendants( children ), | ||
| ]; | ||
| } | ||
| } ); | ||
| return descendants; | ||
| }; | ||
|
|
||
| const childCatIds = getAllDescendants( selectedCatIds ); | ||
|
|
||
| catIds = [ | ||
| ...new Set( [ ...selectedCatIds, ...childCatIds ] ), | ||
| ]; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not recommended to iterate over values inside the useSelect hook callback. For additional details, see: https://make.wordpress.org/core/2025/03/12/data-a-helpful-performance-warning-for-developers-in-the-useselect-hook./
Closes #65332
What?
This PR updates the Latest Posts Block to display posts from both parent and child categories when filtering by a parent category.
Why?
Currently, when using the Latest Posts Block with a Category filter, posts assigned only to child categories are not displayed when filtering by their parent category. This is inconsistent with WordPress's default behavior on category archive pages, where parent category pages display posts from both the parent and all child categories.
This change makes the Latest Posts Block behavior match the expected WordPress category hierarchy behavior, providing a more intuitive experience for users.
Testing Instructions
Screenshots or screencast
Latest.Posts.Block.mov