Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8665961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:36:51+00:00 2026-06-12T17:36:51+00:00

i am looking for a function to fetch wordpress category hierarchy from wordpress tables

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:36:52+00:00Added an answer on June 12, 2026 at 5:36 pm

    It’s not that hard. First I’d wrap that recursive stuff by letting it behave like a RecursiveIterator:

    class RecursiveCategoryIterator implements RecursiveIterator {
        const ID_FIELD = 'term_id';
        const PARENT_FIELD = 'parent';
    
        private $_data;
        private $_root;
        private $_position = 0;
    
        public function __construct(array $data, $root_id = 0) {
            $this->_data = $data;
            $this->_root = $root_id;
        }
    
        public function valid() {
            return isset($this->_data[$this->_root][$this->_position]);
        }
    
        public function hasChildren() {
            $subid = $this->_data[$this->_root][$this->_position][self::ID_FIELD];
            return isset($this->_data[$subid])
                && is_array($this->_data[$subid]);
        }
    
        public function next() {
            $this->_position++;
        }
    
        public function current() {
            return $this->_data[$this->_root][$this->_position];
        }
    
        public function getChildren() {
            return new self($this->_data,
                $this->_data[$this->_root][$this->_position][self::ID_FIELD]);
        }
    
        public function rewind() {
            $this->_position = 0;
        }
    
        public function key() {
            return $this->_position;
        }
    
        public static function createFromResult($result) {
            $menu_array = array();
            while($row = mysql_fetch_assoc($result)) {
                $menu_array[$row[self::PARENT_FIELD]][] = $row;
            }
    
            return new self($menu_array);
        }
    }
    

    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 RecursiveIterator as a mock (for example a RecursiveArrayIterator).

    Now the second part, the actual import of the word-press data:

    // your original query
    $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, $dbh);
    
    // always test for failure
    if($result === false) {
        die("query failed: ". mysql_error());
    }
    
    // create the iterator from the result set
    $wpterms = RecursiveCategoryIterator::createFromResult($result);
    
    // and import it. 
    insert_it($wpterms, 0);
    
    // the function which does all the dirty work.
    function insert_it($iterator, $parent_id = 0) {
        foreach($iterator as $row) {
            // insert the row, just edit the query, and don't forget
            // to escape the values. if you have an insert function,
            // use it by all means
            $qry = 'INSERT INTO my_table (myparent, myname, ...)'
                . ' VALUES (\'' . mysql_real_escape_string($parent_id)
                . '\', \'' . mysql_real_escape_string($row['name']) . '\', ....)';
    
            $status = mysql_query($qry);
    
            if($status === false) {
                // insert failed - rollback and abort
                die("hard: " . mysql_error());
            }
    
            // you need to pass the id of the new row
            // so the "child rows" have their respective parent
            $cid = mysql_insert_id();
    
            // insert the children too
            if($iterator->hasChildren()) {
                insert_it($iterator->getChildren(), $cid);
            }
        }
    }    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a function that calculates years from a date in format: 0000-00-00.
I am looking for a function that would search for the closet 2 elements
I'm looking for a function that reverses clojure hiccup so <html></html> turns into [:html]
I'm looking for some function to determine opening and closing virtual keyboard on samsung
I'm looking for a function to allow me to print the binary representation of
I am looking for a function that would be the alphabetic equivalent of is_numeric.
I'm looking for a function identical to DateTime::createFromFormat but I need it to work
I am looking for a function to reverse any string ( YYYYMDD , YY/MM/DD
I am looking for a function that will tell me, for a list of
I am Looking for a Function that insert Any form values to mysql ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.