Skip to content

Conversation

@dhruvikpatel18
Copy link
Contributor

@dhruvikpatel18 dhruvikpatel18 commented Apr 2, 2025

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

  1. Create multiple categories with parent-child relationships
  2. Create posts assigned only to the child categories (not to the parent)
  3. Add a Latest Posts Block to a page
  4. Select only the parent category in the block settings
  5. Verify that posts from both the parent and child categories appear in the block

Screenshots or screencast

Latest.Posts.Block.mov

@dhruvikpatel18 dhruvikpatel18 marked this pull request as ready for review April 2, 2025 11:08
@github-actions
Copy link

github-actions bot commented Apr 2, 2025

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: dhruvikpatel18 <dhruvik18@git.wordpress.org>
Co-authored-by: justintadlock <greenshady@git.wordpress.org>
Co-authored-by: annezazu <annezazu@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@Mamaduka Mamaduka added [Type] Enhancement A suggestion for improvement. [Block] Latest Posts Affects the Latest Posts Block labels Apr 2, 2025
Copy link
Member

@Mamaduka Mamaduka left a 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.

Comment on lines +127 to +162
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 ] ),
];
}
Copy link
Member

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./

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Latest Posts Affects the Latest Posts Block [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Latest post block: show child categories when parent category is selected

2 participants