How to display posts under a specific subcategory of a given category

after a long search online I have decided to post my problem here in the hope of getting a solution to this issue. Well, I have a custom post with various categories (novels, books, magazines etc..) and each of these categories have many subcategories (2002, 2004, 2005, 2006 etc) the year of publication. Basically what I need is to show a filtering menu based only on the relevent subcategories of the selected category. And when the user clicks on a subcategory menu item he will be able to get all the posts under that specific category.

this is what I have done so far, but I could no find out how filter by subcategory while in a given category.

Read More
        if (isset($_GET['cat'])){
    $subcat = $_GET['cat'];
    query_posts( 'posts_per_page=10&paged='.$paged.'&post_type=publication&publication_category='.$maincat.**'&category__in='**.$subcat );
        }else{
    query_posts( 'posts_per_page=10&paged='.$paged.'&post_type=publication&publication_category='.$maincat );   
    }

I be so thankful to anyone who could give me a bit of his time to solve this problem.

Related posts

Leave a Reply

1 comment

  1. It should be enough to do this:

    query_posts( 'posts_per_page=10&paged='.$paged.'&post_type=publication&publication_category='.$subcat );
    

    If you look at your original code you can see why you went wrong by looking at what you are literally asking in your arguments:

    I would like posts that are in the main category but are in the child category.

    When what you want is:

    I would like posts that are in the child category

    Also, make sure to use term IDs not term Names, as they’re more reliable.

    Also a final note of crucial importance

    You’re checking against cat, but this is a reserved WordPress name, and will only ever hold a category ID, not a publication_category ID. If you pass anything using this GET variable, WordPress will modify the query accordingly assuming its a category. This is bad news.

    So you can either rename the variable used, or, use the taxonomy archives which si what your code is hinting at. e.g. example.com/publication_category/maincategory/subcategory/ and have WordPress do all this for you.