0

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!

1 Answer 1

2

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.

5
  • 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, 2012 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.
    – mor7ifer
    Feb 1, 2012 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, 2012 at 15:15
  • Register taxonomy is only for custom taxonomies, you can use the built in taxonomies (tags and categories) as well.
    – mor7ifer
    Feb 2, 2012 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, 2012 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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