How to check if a category has a parent category?

Im trying to check if ‘categoryone’ has a parent.
Right know I can check and see that there is a category called categoryone, but not if categoryone has a parent category.
I have tried to code something like the code bellow.

  $tid = term_exists('categoryone', 'category', 0);

  $term_ids = [];

  if ( $tid !== 0 && $tid !== null )
  {
$term_ids[] = $tid['term_id'];

  }
  else
  {
    // If there is not a parent category!
    $insert_term_id = wp_insert_term( 'categoryone', 'category' );
    if ( ! is_wp_error )
    $term_ids[] = $insert_term_id;
  }
  wp_set_post_categories( $insert_id, $term_ids );

Related posts

Leave a Reply

7 comments

  1. You may use something like this (Paste this in your functions.php file)

    function category_has_parent($catid){
        $category = get_category($catid);
        if ($category->category_parent > 0){
            return true;
        }
        return false;
    }
    

    Call this from template

    if(category_has_parent($tid)) {
        // it has a parent
    }
    

    Check Children

    function has_Children($cat_id)
    {
        $children = get_terms(
            'category',
            array( 'parent' => $cat_id, 'hide_empty' => false )
        );
        if ($children){
            return true;
        }
        return false
    }
    

    Call this from template

    if(has_Children($tid)) {
        // it has children
    }
    
  2. You can use the get_category() to fetch current category details by passing term_id

    $tid = term_exists('categoryone', 'category', 0);
    $t_details=get_category($tid);
    if(!empty($t_details->parent)){
    echo $t_details->parent; // parent category id or $t_details->category_parent
    }
    

    get_category() returns the object like below taken from reference site

    stdClass Object
    (
        [term_id] => 85
        [name] => Category Name
        [slug] => category-name
        [term_group] => 0
        [term_taxonomy_id] => 85
        [taxonomy] => category
        [description] => 
        [parent] => 70
        [count] => 0
        [cat_ID] => 85
        [category_count] => 0
        [category_description] => 
        [cat_name] => Category Name
        [category_nicename] => category-name
        [category_parent] => 70
    )
    

    EDIT To get the child categories you can use get_categories() by passing the the arguments array.

    parent

    (integer) Display only categories that are direct descendants (i.e.
    children only) of the category identified by its ID

    $args=array('parent'=>$tid);
    $child_categories=get_categories($args);
    
  3. Well you can use get_category_parents() (take a look at the link to read over the arguments, etc). This will return a list of all the parents in hierarchical order. In that case, if you just want the most immediate parent category you could do something like this:

    <?php 
     $parent_cats = get_category_parents( $cat, false, ',' ); 
     $parent_cat = explode(",", $parent_cats); 
     echo $parent_cat[0];    
    ?>
    

    This will echo out the 1st parent category (the one directly above your current category).

    To be clear:

    in get_category_parents()

    – arg 1 is the category id (i just used $cat arbitrarily)
    – arg 2 is if you would like wp to add a link to each returned parent category
    – arg 3 is the delimiter used to separate the returned categories, if any

    in explode()
    – arg 1 is the delimiter to look for to separate the string in an array
    – arg 2 is the source string to separate

    Happy coding!

  4. Use below code to check parent and children of a category.

    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); // get current term
    
    $parent = get_term($term->parent, get_query_var('taxonomy') ); // get parent term
    
    $children = get_term_children($term->term_id, get_query_var('taxonomy')); // get children
    
    if(($parent->term_id!="" && sizeof($children)>0)) {
    
       // has parent and child
    
    }elseif(($parent->term_id!="") && (sizeof($children)==0)) {
    
       // has parent, no child
    
    }elseif(($parent->term_id=="") && (sizeof($children)>0)) {
    
       // no parent, has child
    
    }
    
  5. @ M Khalid Junaid

    To expand on your answer, I used the basis of your code to check if it the category has no parents and is a top tier item in a category/taxonomy heriarchy. I am including this because it is what I was looking for when I stumbled across this thread and maybe someone else is doing the same.

    <?php
    $args = array( 
        'taxonomy' = 'categories'; // If using custom post types, type in the custom post type's name
    );
    
    $terms = get_terms( $args );
    
    foreach ( $terms as $term ) {
        $has_parent = $term->parent;
        $name = $term->name;
    
        // If it doesn't have parents...
        if ( !$has_parent ) {
    
            // ...then it's the top tier in a hierarchy!
            echo "Tier 1:" $name . '<br />';
    
        }
    }
    ?>
    

    Output

    Tier 1: Category Name
    Tier 1: Another Category Name
    Tier 1: All Top Level Categories

  6. First of all, get all the terms under your taxonomy ‘taxonomy_name’ and
    then check each of them has parent or not.
    Try out the code below

    <?php
    $service_terms= get_terms( array(
        'taxonomy' => 'taxonomy_name'
    ) );
    ?>
    <?php if(!empty($service_terms)) : ?>
       <?php foreach($service_terms as $term) : ?>
            <?php $has_parent = $term->parent; ?>
            <?php if (!$has_parent): ?>
                          <!-- If this term is a parent then put your code here -->
            <?php endif ?>
       <?php endforeach ?>
    <?php endif ?>