To display the latest 4 post titles as links on your homepage, limited to posts where the parent category is “Category name,” you’ll need to use WP_Query
and specify the parent category for filtering, as shown below.
✅ Method: Retrieve Posts from the Parent Category “Category name”
Step 1: Get the ID of the Parent Category “Category name”
First, obtain the ID of the parent category “Category namel” (e.g., let’s assume the ID is 10
).
*You can find the actual ID on the “Posts > Categories” screen’s link URL, or by using functions like get_category_by_slug()
.
term_id; ?>
Step 2: Get Posts Including Child Categories of the Parent Category “Category name”
term_id; $args = array( 'posts_per_page' => 4, 'post_type' => 'post', 'orderby' => 'date', 'order' => 'DESC', 'category__in' => get_term_children($parent_id, 'category'), ); $query = new WP_Query($args); if ($query->have_posts()) : ?>
No articles found.
🔍 Key Points
get_term_children($parent_id, 'category')
retrieves an array of child category IDs for “Category name.”- Passing this to
category__in
fetches only articles within the child categories of “Category name.” - If you also want to include posts directly assigned to the “Category name” category itself, you can merge them by using
$category_ids = array_merge([$parent_id], get_term_children($parent_id, 'category'))
.
Example: Including Parent “Category name” + Its Child Categories
term_id], get_term_children($parent->term_id, 'category')); $query = new WP_Query([ 'post_type' => 'post', 'posts_per_page' => 4, 'orderby' => 'date', 'order' => 'DESC', 'category__in' => $category_ids, ]);