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 6740571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:37:17+00:00 2026-05-26T11:37:17+00:00

A WordPress function should return the top level term id of a given child

  • 0

A WordPress function should return the top level term id of a given child term id – terms can have 1..n parents, so a recursive function seems to be useful here.

// Recursive function
function return_top_level_term($term_id, $taxonomy_name) {
    $term = get_term_by('id', $term_id, $taxonomy_name);
    if($term->parent>0) {
        return_top_level_term($term->parent, $taxonomy_name);
    } else {
            // Here we get the correct value
        return $term->term_id;
    }
}

PHP indeed does find the correct term_id, but the function always returns false.

$my_top_level_term = return_top_level_function(423, $tax);

Example with three layers, informal notation:

return_top_level_term(return_top_level_term(return_top_level_term(return 1;)return false;) return false;)

I am searching for the 1, but false is always returned, although the the function does not have a return value.

Of course I could write a local variable above the function which can save the value because of the scope rules, but I want to write it into a library – is there a way of returning this value by calling the recursive function?

  • 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-05-26T11:37:17+00:00Added an answer on May 26, 2026 at 11:37 am

    Change this line

    return_top_level_term($term->parent, $taxonomy_name);
    

    to

    return return_top_level_term($term->parent, $taxonomy_name);
    

    Otherwise, the last result will not be returned to the top function on the stack and the return will be void.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There seems to be a bug in a Wordpress PHP function that leaves whitespace
I have written a function for a multilevel wordpress menu, but I'd like it
im making a wordpress plugin and i have a function where i import images,
I have used the remove_action function in Wordpress before to remove junk from the
I am trying to customise a Wordpress theme. I have a function in themes/functions.php
i have to create a customized wordpress theme. i still don't how it should
I have a function for language (like the system that Wordpress and other CMSes
I am wondering if wordpress' insert function also adds slashes to data. If it
I am trying to modify a wordpress / MySQL function to display a little
i want to know what wordpress build-in function you will use when sanitizing specific

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.