Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

        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.

share|improve this question
add comment

1 Answer

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.

share|improve this answer
    
Thanks a lot for your quick reply. I forgot to tell you that the subcategories of other categories may have the same name in this case (year 2002, 2004 etc...) for example. I have already tried your solution but the iconvenient is that it also gets posts of subcategories with the same year while belonging to another category. I hope that you got the idea of what I am trying to do, and any suggestion will be very much apreciated. –  Musta Nov 21 '11 at 12:03
    
Instead of using the year '2002', use the term ID, the term ID is always unique. The Term ID is an integer used in the database, and it is what WordPress uses to handle a taxonomy term, it provides named term APIs purely as a helper method. Use Term IDs for precision –  Tom J Nowell Nov 21 '11 at 15:34
1  
This did the trick. I was so blind. Thanks alot indeed –  Musta Nov 22 '11 at 10:30
    
Glad to be of help =] –  Tom J Nowell Nov 22 '11 at 11:03
    
( Mark question as accepted? ) –  Tom J Nowell Nov 22 '11 at 11:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.