get_terms($taxonomy, array('parent' => $parent));

This works for some parents and returns an empty array for others. I checked if a term with the parent ID exists and if the same ID is in the 'parent' field of the child term's taxonomy. Even copy-pasted directly from the database. Is there anything else? I don't see a difference between the parents that work and the ones that don't.

edit: All invisible terms appear the moment I add a new term via the dashboard. Turns out the problem was that there is a function wp_insert_term() for adding terms and I was using $wpdb->insert and manually inserting the term and the taxonomy, which makes a difference, somehow.

  • 2
    Your question makes no sense at all. Please rephrase Jan 7, 2016 at 16:57
  • 1
    What is the output of get_terms and what type is the variable $parent ?
    – Laxmana
    Jan 7, 2016 at 17:15
  • @PieterGoosen Let's say there are a few terms with a parent with an ID of 1 and a few terms with a parent with an ID of 5, both have the taxonomy "board". get_terms("board", array('parent' => 1)); works and get_terms("board", array('parent' => 5)); doesn't. A term with an ID of 1 exists and a term with an ID of 5 exists. Why would the second code not work? What makes a term valid in wordpress' eyes?
    – Nadroev
    Jan 7, 2016 at 17:18
  • @Laxmana It's a wordpress function. It returns an array of term objects. $parent is an integer.
    – Nadroev
    Jan 7, 2016 at 17:21
  • Sorry let me rephrase my question. I know it is a wordpress function. I meant what is the output of get_terms when it "fails". Is a WP_Error or an empty array?
    – Laxmana
    Jan 7, 2016 at 17:26

2 Answers 2


The only variable which can really have an effect on the result as you described, is taxonomy. If you have verified that there are terms with a parent of 1 and a parent of 5, and get_terms() only returns terms for terms with a parent of 1, it can only mean that term 1 and term 5 belongs to different taxonomies. get_terms() will only return results if the term data passed is associated with the passed taxonomy.

You need to remember, all terms, regardless of taxonomy, are stored in the wp_terms table. Relationships are stored in the wp_term_taxonomy table. This is where the term's taxonomy and parent is saved.

Here is a small script which you can run on any page. This will print all the taxonomies and all terms associated with that taxonomy and also print the parent ID if the terms aren't top level terms. You can use this to "debug" your issue

$taxonomies = get_taxonomies(); 
if ( $taxonomies ) {
    foreach ( $taxonomies as $taxonomy ) {
        $terms = get_terms( $taxonomy );
        if ( $terms ) {
            echo '<strong>' . strtoupper( $taxonomy ) . '</strong></br>';
            foreach ( $terms as $term ) {
                $parent_id = ( 0 != $term->parent ) ? ' and has a parent ID of ' . $term->parent : '';
                echo 'Term '. $term->name . ' ID ' . $term->term_id . ' belongs to the taxonomy '. $taxonomy . $parent_id . '</br>';
  • I know about wp_terms and wp_term_taxonomy, can't really check all of them with your code, since they are a few thousand, but still checked the taxonomies in case there are two with similar names, but no, every term I am working with belongs to the same taxonomy. I thought it could be because the IDs for the problematic ones are bigger than 9999, but I made a new one via the admin dashboard and it's showing normally.
    – Nadroev
    Jan 8, 2016 at 8:46
  • Then you have an issue with a bad filter in your theme or plugin as this has nothing to do with default behavior. WordPress does not "favor" certain terms, they all work exactly the same, specially within the same taxonomy. Start by clearing ALL caches, browser and plugin, if the problem persist, start by deactivating one plugin at a time and check when the problem stops. If this does not work, change to a bundled theme. Also, download a plugin to repair and optimize your db, there might be misplaced data as well. Jan 8, 2016 at 9:37

Use wp_insert_term() instead of $wpdb->insert for adding new terms.

  • if you look at the source code for wp_insert_terms, it does a huge amount of work before a term is successfully added, which $wpdb->insert does not do. Even when something goes wrong, the term is inserted with $wpdb->insert, and this causes issues with relationships. The Wordpress functions might look heavily overboard, but it is done for a reason. Never use plain SQL to do complex stuff like inserting terms Jan 8, 2016 at 10:52

Your Answer

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

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