• Well, first of all hello to all and excuse me for my poor english!

    I have done a little hack to be able to show posts from conditional categories… Let me put a very easy to understand example: real estate. Imagine I have a category called Properties with subcategories called Homes, Farms, Cabins and so on. Now, I want all properties to be submitted to one of those categories AND… to one one of the subcatgeories in the category State. This is another category with subcategories like Arkansas, California… So all posts will be under two categories, the property type category (Homes, Farms…) and the state category (Arkansas…).

    Now, I can browse any given property category, like Homes, and I can see all posts under this category (well, that’s what categories are for, aren’t they?), no matter what state category they belong to. If I browse a state category, say Arkansas, I get all the posts from this given category no matter what property category they belong to (Homes, Farms…). Fine, that’s what I expect.

    But… what if I want to mix/cross both types of categories? What if I only want to browse Homes from Arkansas?

    THE HACK

    Maybe there is a simpler way to do this, maybe this is something allready implemented in WP and I just missed (passed?) it, so feel free to call this hack stupid, cumbersome, wrong, a waste of time, poorly coded (I’m not a PHP expert) or anything… Anyway, this is what I did:

    In the header of the blog I have links to property categories (Homes, Farms…) and in category.php I load a custom sidebar where I have placed a list of states. It is a dynamic list generated this way:

    <ul><?php
    $title = get_catname($cat);
    $cats = get_categories('child_of=10&hide_empty=0');
    $count=1;
    foreach ((array)$cats as $category) {
    if ($state && $state == $category->category_nicename) {
    $catlist = '<li class="current">'.$category->cat_name.'</li>';
    } else {
    $catlist = '<li><a href="'.get_category_link($cat).'?state='.$category->category_nicename.'" title="'.$title.' in '.$category->cat_name.'">'.$title.' in '.$category->cat_name.'</a></li>';
    }
    echo $catlist ;
    $count++;
    }
    ?></ul>

    As you can see, it takes all children categories of category #10 (State), and creates links with the URL of the current category plus the parameter (is that the word?) STATE, so if I’m browsing the Homes categories, links in the sidebar look like this:

    Homes in Arkansas
    Homes in California

    And so on. And the URL is like this:

    site.com/category/properties/homes?state=arkansas
    site.com/category/properties/homes?state=california

    Now, in category.php I look for that parameter STATE, like this:

    $state = (isset($_GET['state'])) ? $_GET['state'] : "";

    And once I get it, I can start apllying changes, like in:

    if ($state) { ... }

    Then, I get the ID of the state:

    $state_id = get_cat_id($cat_name=$state);

    And modify the loop with a custom query:

    query_posts(array(
    'category__and' => array($cat,$state_id)
    ));

    This way I get only posts that are in both the current category and the given state category. So, if I click on:

    site.com/category/properties/homes?state=arkansas

    The result is that I only get posts that are in both Homes and Arkansas category. Nice.

    Now, the final trick is that in the list generated in the sidebar (the first code I posted) I made it so the current state category is highlighted (with custom HTML and CSS) and not linked (to avoid linking to itself). It’s this part:

    if ($state && $state == $category->category_nicename) {
    $catlist = '<li class="current">'.$category->cat_name.'</li>';
    } else {
    $catlist = '<li><a href="'.get_category_link($cat).'?state='.$category->category_nicename.'" title="'.$title.' in '.$category->cat_name.'">'.$title.' in '.$category->cat_name.'</a></li>';
    }

    Well, everything works and hopefully this is something others can benefit from. Or, as I said, maybe you know of a better, easier way.

    THE QUESTION

    I’m not into real estate and I’m not based/focused on the USA market (so I have no use for all these state categories). In fact, and this is the problem, I have a more complex category structure and because of this, and what I want to acomplish, sometimes I feel WP is not the right platform for me. But the only problem I have with WP is with conditional categories so… should I ditch such a great software just because of ONE and only ONE problem? Maybe it is a too big problem, but I really think it must be worth to look for a solution.

    What’s the problem then? Back to real state categories (they are really easy to understand) what if under Homes I have two categories like New and Used? This may or may not work:

    query_posts(array(
    'category__and' => array($cat,$state_id)
    ));

    Why? Because, as in the previous example, it will show post from Homes that also belong to a given state category (Arkansas in the example): posts from Homes that were also posted in Arkansas… What if I don’t have posts in Homes? What if I directly submit posts to either New or Used categories (the ones bellow Homes)? It won’t work. I will get zero results.

    Even if the category Homes is empty, if I click the link

    site.com/category/homes

    I’d get posts from the children categories (New and Used), but if I click

    site.com/category/homes?state=arkansas

    I will get nothing. Obviously, this is because the Homes category is empty (posts are in the children categories) and in the query

    query_posts(array(
    'category__and' => array($cat,$state_id)
    ));

    I’m only asking for posts in the Homes category. The big question is… how to solve this?

    Well, I can get the children categories array with

    $cats = get_term_children($cat, 'category');
    $cats[] = $cat;

    but… how do I pass this to the query? This is my BIG question. How do I make a conditional query with those children categories? Something like

    query_posts(array(
    'category__and' => array($cat,$state_id,$sub_cats)
    ));

    won’t work, because there is not a single post belonging to ALL those categories, and something like

    query_posts(array(
    'category__in' => array($cat,$state_id,$sub_cats)
    ));

    will show ANY posts from ANY of the categories no matter what relation they have (for example, it will show ALL posts from Arkansas, or ALL posts from New or Used, EVEN if they don’t belong to the Homes category).

    Well, sorry for a long post and I hope you understood what I said because I’m really lost (my english doesn’t help), I’m very new to PHP and believe me, I tried anything (reading hundreds of posts, burning Google and using the old good try and error technique -that has its limitations).

    Thaks for your time and thanks in advance for any help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter mortadelo

    (@mortadelo)

    Any suggestion?

    I’ve been trying to modify the query_posts with category conditional tags, as in

    if (is_category(homes)) { query_posts... }

    without success.

    Well, the problem is that, AFAIK, there is not something like IF or OR (||) for query_posts, just AND and IN:

    query_posts(array(
    'cat' => some_cats,
    'category__and' => array($some_cats),
    'category__in' => array($some_cats)
    ));

    OK, following the example from my first post, if a follow the link

    site.com/category/homes?state=arkansas

    I should get posts that are from the category Homes OR from any of its subcategories (New and Used) AND that also belong to the category Arkansas.

    With

    query_posts(array(
    'cat' => the_id_of_Homes
    ));

    I get posts from Homes and its subcategories (New and Used). With

    query_posts(array(
    'cat' => the_id_of_Homes,the_id_of_Arkansas
    ));

    I get posts from both Homes (and its subcategories) and posts from Arkanasas, BUT not posts from Arkansas that only belong to Homes or its subcategories (just ALL posts from Arkanasas), and the same will happen if I use the category__in option. With

    query_posts(array(
    'category__and' => array(the_id_of_Homes,the_id_of_Arkansas)
    ));

    I get posts that are from both Homes and Arkansas, BUT no posts from Homes’ subcategories. With

    query_posts(array(
    'category__and' => array(the_id_of_Homes,the_id_of_Homes_subcategories,the_id_of_Arkansas)
    ));

    I’ll get nothing since it will try to find posts that belong to ALL those categories, what is unlikely.

    So… any idea on how to get posts that are from the category Homes OR from any of its subcategories (New and Used) AND that also belong to the category Arkansas? Any advice would be very much appreciated.

    Oh, and sorry for the long post, but writing all this is helping me to better understand what I’m doing or trying to do. Hope you dont mind.

    Regards.

    Thread Starter mortadelo

    (@mortadelo)

    Well, this almost did the trick: making the loop show all posts from the state categorie and adding

    if ($region) {
    if (!in_category($cat)) continue;
    }

    won’t display posts that weren’t submitted to the current category or subcategory, so if you are browsing

    site.com/homes/new/?state=arkansas

    it will get all posts from arkansas and THEN hide all those that are NOT in category New.

    The problem, is that WP will still fetch ALL the posts from the state category and just HIDE some of them.

    So in order to avoid this unnecesary server load, my initial question is still open…

    But it seems I’m asking for the impossible.

    Thread Starter mortadelo

    (@mortadelo)

    Anyone?

    Thread Starter mortadelo

    (@mortadelo)

    You win. I move forward. I’m impressed by WP support. You can keep answering to posts asking the same things over and over (and things already covered somewhere in the documentation) or to those popular posts asking for CSS help (yes, it seems this is a good place for CSS support, but for WP itself…). Good bye. Best wishes.

    It seems you found a solution but you are concerned about the server load. Unfortunately, I don’t know that you are going to get a better answer than the one you proposed.

    Hopefully the lack of response in the thread doesn’t turn you off to the forums–we need people of your skills!

    You might also find some solace in joining the WordPress wp-hackers mailing list or checking out the #wordpress IRC.

    Thanks for posting your thoughts and solution.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Conditional categories hack (and a question)’ is closed to new replies.