-1

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Blackbam
  • 17,496
  • 26
  • 97
  • 150
  • I would suggest avoiding naming your functions starting with `return_`. Would likely make this kind of error more obvious and makes the code a bit more readable. – Michael Mior Oct 24 '11 at 20:36

3 Answers3

6

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sharpner
  • 3,857
  • 3
  • 19
  • 28
  • 1
    Yes. This is why you don't name your functions like this, it will just end up confusing you at some point. – Logan Serman Oct 24 '11 at 20:36
  • Indeed. The function actually doesn't return false, but it returns nothing. For the last call, the id is returned, but the result is passed to the previous call which should also return it. It is the first call of the recursion that eventually passes the result to the calling code. – GolezTrol Oct 24 '11 at 20:37
  • By changing the function the way sharpner has indicated, your results should bring back that (1) all the way from the bottom of the stack. – uotonyh Oct 24 '11 at 20:42
  • think of recursion as if you were putting functions on top of a pile, when reaching the last function you would add and you return a result each function on the pile should return the last functions result, so the value reaches the top and the result will finally be returned... well hope it helps xD – sharpner Oct 24 '11 at 20:49
0
// 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 return_top_level_term($term->parent, $taxonomy_name);
    } else {
        return $term->term_id;
    }
}

You may have missed a return statement.

This can be written also as:

// 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 return_top_level_term($term->parent, $taxonomy_name);
    }

    return $term->term_id;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Salvatore Previti
  • 8,956
  • 31
  • 37
0

You're not returning the value on the recursive function call.

You should have return return_top_level_term($term->parent,$taxonomy_name); (note the extra return)

Michael Mior
  • 28,107
  • 9
  • 89
  • 113