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.

What I'm Doing

I have a cities category and normal categories. Right now I have them filtering by category, then when the user clicks a city it will filter by category AND city

query_posts(array( 'category__and' => array($city, $category))

The Problem

That works - no problem. The problem comes when I add sub-categories to my main $categories and the admin does not check the parent.

Example

I have a main category: Food and 2 sub-categories Fries & Burgers. I also have a city category called New York. The admin adds a post and checks Fries and New York so by default wordpress will pull up this post. When the user tries to filter it by city (New York) it says there's no posts because the parent was never checked.


How do I Bring that sub-category post to the top when the user filters the parent category by city?

I apologize if it is confusing so ask questions if needed. Thanks in advance!

share|improve this question

1 Answer 1

up vote 2 down vote accepted

category__and is exclusive, so it will not work for your application, if you use tax_query and setup your query as follows, it should work:

$args = array(
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'category',
            'field'    => 'id',
            'terms'    => $city,
            //'include_children' is true by default
        ),
        array(
            'taxonomy' => 'category', //may need to be categories, I don't recall, tweak it till it works.
            'field'    => 'id',
            'terms'    => $category,
            //'include_children', again, is true by default
        )
    )
);
$posts = query_posts( $args );

If that doesn't sit well with query_posts() (I don't see any reason it wouldn't), WP_Query won't complain, so just use that. Also, that link to WP_Query has all the docs you'll need to modify that query, so thumb through there, you'll probably at least want 'post_status' => 'publish'...maybe some more.

share|improve this answer
    
OK, so this looks like what I need but now I'm just trying to wrap my head around the terms. From my understanding taxonomy would be the parent category, field is the slug ID and terms would be the sub-category? I figured $city / $category would be in taxonomy but maybe I don't fully understand the keywords here. –  Howdy_McGee Feb 1 '12 at 15:38
1  
Did you read the documentation? I don't understand your question. taonomy is things like tags, categories, or the slug of a taxonomy you registered with register_taxonomy(). field is either slug or id, depending on whether you want to use the category ID or the slug as the identifier in terms. terms is your slugs or ids (string or array). I STRONGLY encourage you to read the documentation and just play around with the function, it's not writing to the database, so you can't fubar anything. –  m0r7if3r Feb 1 '12 at 18:48
    
I think I see how taxonomy works (or at least a bit) but it seems that register_taxonomy() is just another step for the admin to take which I am trying to avoid. I mean if I wanted I could just tell the admin to simply check the parent category whenever they make posts but I'm trying to get around that somehow. –  Howdy_McGee Feb 2 '12 at 15:15
    
Register taxonomy is only for custom taxonomies, you can use the built in taxonomies (tags and categories) as well. –  m0r7if3r Feb 2 '12 at 23:13
    
Ohhh I see. Thanks :D ! Taxonomy was pretty confusing until I played around with all the different terms I could put in there and how it worked. I edited your answer to the code that fit my need (that worked for me). Thanks again! –  Howdy_McGee Feb 3 '12 at 14:58

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.