Reputation: 79
This is my structure:
+Parent1
-Child Cat 1
-Child Cat 2
+Parent2
-Child Cat 4
-Child Cat 5
--> 2 posts have been published in child category 1
--> 3 posts have been published in child category 2
--> 7 posts have been published in child category 4
--> 9 posts have been published in child category 5
No posts have been published directly in parent1 and parent2. Published in child category only.
I want the posts of the child category to be displayed in the corresponding parent category.
For example, in parent category 1, all 5 child category posts(2+3) should be displayed.
And in parent category 2, all 16 child posts(7+9) should be displayed.
Also, the priority of displaying posts in the parent category 1&2 should be with sticky posts.
This is my code:
global $post;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
$myposts = get_posts(
array(
'posts_per_page'=>20,
'offset' => 0,
'cat' => $category,
'post_status'=>'publish',
'order'=>'ASC',
'ignore_sticky_posts' => 0
),
);
Upvotes: 1
Views: 861
Reputation: 11533
This is because
get_the_category
only returns the categories that are assigned to that post - and it looks like you are specifically limiting it to the first category.cat
doesn't traverse upward to parent categories. It does return posts of child categories.To do what you want, get the parent
property from the category object:
global $post;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
// category parent
$category_parent = $category[0]->parent;
// Build the category array
$category_array = [ $category, $category_parent ];
$myposts = get_posts(
array(
'posts_per_page'=>20,
'offset' => 0,
'category__in' => $category_array, // set the array here.
'post_status'=>'publish',
'order'=>'ASC',
),
);
You can remove ignore_sticky_posts
since the default value is already false, can you can use category__in
instead of cat
to pass an array of categories.
This is untested, but should work. If it doesn't, try doing:
`'cat' => implode(',', $category_array )`
Instead of the category__in
.
Based on your updated question, use get_term_children
(documentation) or you should just be able to pass the parent category ID to the query, since cat => id
should get the posts of that category, including children.
global $post;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
// category parent
$category_parent = $category[0]->parent;
// Get all the children categories
$children_categories = get_term_children( $category_parent, 'category' );
// Build the category including the parent and the children of that parent.
$category_array = [ $children_categories, $category_parent ];
/* this passes the parent and children categories. */
$myposts = get_posts(
array(
'posts_per_page'=>20,
'offset' => 0,
'cat' => implode(',', $category_array ),
'post_status'=>'publish',
'order'=>'ASC',
),
);
/* ALTERNATIVE: this only passes the parent ID */
$myposts = get_posts(
array(
'posts_per_page'=>20,
'offset' => 0,
'cat' => $category_parent,
'post_status'=>'publish',
'order'=>'ASC',
),
);
Upvotes: 2