i am looking for a function to fetch wordpress category hierarchy from wordpress tables (wp_terms, wp_term_relationships, wp_term_taxonomy) WITHOUT using wordPress functions / templates.
i am basically looking to fetch categories (parent/child) relationship and then I want to insert them into my own CMS table. For this i must know “What Category Comes under What? (with all the sub-sub (multiple) directories)”
So far I made this:
$sql="SELECT a.term_id,a.description,a.parent,a.count,b.name,b.slug
FROM wp_term_taxonomy a INNER JOIN wp_terms b WHERE a.term_id=b.term_id
AND a.taxonomy='category';
";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
echo($row['name'].'<br>');
}
exit;
This function displays all the categories but NOT the hierarchy and i am NOT able to get the Child Parent thing..
Can anyone help me with this please?
Regards
It’s not that hard. First I’d wrap that recursive stuff by letting it behave like a
RecursiveIterator:Now why would I do that? First, because you can re-use id for displaying the tree, or do other stuff with it like import it in your own table. Second, if you have to test your code, you can just put in some other
RecursiveIteratoras a mock (for example aRecursiveArrayIterator).Now the second part, the actual import of the word-press data: