Well, the t2.parent refers to the wp_term_taxonomy database table’s ‘parent’ column. So, “ORDER BY t2.parent DESC” fetches all the terms, with any terms whose ‘parent’ field is zero being put last in the returned array.
Looking back at the code, this was something that I was thinking I might need for the sorting part of the algorithm, but later didn’t take advantage of it (I’d want to doublecheck that to make sure before I throw it out though). Maybe what I should do is have the call order by the wp_term_relationships ‘term_order’ field, which at first glance I think is the order of appearance of a term as set by the user, with zero being at the top or unsorted, and I’m guessing anything with a greater number has greater ‘weight’, making it sink lower in a list, like how Drupal sorts its menu items?
Anyway, this probably won’t address the entire solution once the terms are all sorted into their hierarchies, only for terms in the top of the hierarchy, so I might need to rethink that when I rethink the sorting algorithm, which I’ve been planning to do as it’s a little cumbersome at the moment and has obvious areas of improvement.
I’m trying to stick to an algorithm that will build the term tree after only calling the database 1 or 2 times, rather than the commonly-used algorithm that calls the database with every parent term, making it very poor in performance. Anyway, hopefully I can get some time for that soon, but I’m starting a new job on Monday, so if anyone would like to help with this, let me know.
This is very premature for me to say, and it’s an extremely untested solution, but you might try replacing that phrase you mentioned:
SELECT t1.name, t1.term_id, t2.parent FROM wp_terms t1, wp_term_taxonomy t2, wp_term_taxonomy t3 WHERE t2.term_id = t1.term_id AND t3.term_id=t1.term_id AND t3.taxonomy=”category” ORDER BY t2.parent DESC
with
SELECT t1.name, t1.term_id, t2.parent FROM wp_terms t1, wp_term_taxonomy t2, wp_term_taxonomy t3, wp_term_relationships t4 WHERE t4.object_id = t1.term_id AND t2.term_id = t1.term_id AND t3.term_id=t1.term_id AND t3.taxonomy=”category” ORDER BY t4.term_order ASC
and re-arrange the terms in the admin section and see if that consequently re-arranges the terms on the widget
Thanks a lot for the tip, Andrew – it didn’t quite do the trick, but it did get me to learn about accessing the database tables and filling in the term_order column in the wp_terms table.
In the end, I just made this simple little switch to the code:
‘ORDER BY t1.term_order ASC’
Works perfectly. I’m labeling parent categories in the 1000s, and child categories in the 100s – this leaves me lots of room to insert numbers of new categories down the road.
One more quick question:
I’ve been using a similar set up that had a slice of code in it that meant top-level categories wouldn’t have checkboxes, and I could also style them to look like headers for the sub cats. The code looks like this:
‘if ($subcat_level == 0)
echo “<h4>”.$a_cat->name.”</h4>”;
else’
Can you think of how I could implement something similar inn your code?
Thanks a lot for all your help.
Max
Ok, I’ve got good news and bad news for you:
first, the bad news is I took a closer look at term_order, and I think it actually is supposed to signify the order of display of tags in an article, but if you’re ok with messing with that, that’s fine.
The good news is your second request is fairly easy. Just replace line 380 with an ‘if’ statement as follows:
if($tag['level'] == 0)
{
$returnstring1 = $tag['name'];
}
else
{
$returnstring1 = $returnstring1 = '<li><label for="searchterm[]">'.$tag['name'].'</label><input type="checkbox" name="searchterm[]" value="'.$tag['term_id'].'" /> </li>';
}
Andrew – that’s fantastic, thanks! Works perfectly.
As for the term order thing, that’s not a problem (and either way, I haven’t actually seen it have any effect anywhere besides the faceted search widget).
Okay. This is the LAST question, promise: Can you think of a way to get the widget to repopulate itself with categories relevant to ones that have already been selected? For example – I run a small animation studio. Here’s what a sample of my search widget is populated with:
Job Type
– 2d
– 3d
Subject
– Characters
– Backgrounds
– Logos
So, ideally, if a client clicks the 3d box, but I only have posts marked with the category combinations: ‘3d / Characters’, and ‘3d / Backgrounds’, then ‘Logos’ would disappear from the list.
This would keep people from getting any empty page results due to searching for combos that don’t exist.
Thanks again;
Max