1
votes

I have write a theme for WordPress and I like to have the latest posts posted in category and it's sub-categories to be displayed on top of any other post.

An example. Lets say I have the following categories:

Cat 1
    Cat 1 - 1
    Cat 1 - 2
        Cat 1 - 2 - 1

And then I create the following posts :

Post #5   |   Cat 1 - 2       | Date 2013
Post #4   |   Cat 1 - 1       | Date 2012
Post #3   |   Cat 1 - 2       | Date 2011
Post #2   |   Cat 1           | Date 2010
Post #1   |   Cat 1 - 1 - 2   | Date 2009

In the front end, when I navigate to Cat 1 I do not get as latest post the Post #5 that belong to Cat 1 - 2 where it is sub category of the Cat 1, But instead I am getting the Post #2.

Currently I am using this code :

$categoryID         =   get_query_var('cat');

$args   =   array(
    'post_type'         =>  'post',
    'posts_per_page'    =>  1,
    'category__in'      =>  array($categoryID),
    'post_status'       =>  'publish'
);

$eiPost =   new WP_Query($args);

The problem is that this code return the latest post only from the top level category and not from the sub categories. How can I modify this code, in order to retrieve the latest posts from all the sub-categories and the top category ?

Is there any solution to this problem ?

1

1 Answers

2
votes

'category__in' only displays posts from that category, not children categories.

Try using 'cat' => $categoryID insted. So your $args would be:

$args   =   array(
'post_type'         =>  'post',
'posts_per_page'    =>  1,
'cat'      =>  $categoryID,
'post_status'       =>  'publish'
);