0

I have been developing the shortcodes for which am pulling the terms of custom taxonomies from a plugin. It was working fine prior to WordPress 4.5.0 and now realized it is not working after 4.5.0.

I am pulling those terms in an old method like below.

Old Method

// Sermon Topics
$prefix_topic_terms= array();
if ( ! empty( $prefix_topic_terms ) ) {
    $prefix_sermon_topic = get_terms('ctc_sermon_topic', 'orderby=name&hide_empty=0');
    foreach ($prefix_sermon_topic as $category) {
        $prefix_topic_terms[$prefix_sermon_topic->slug] = $prefix_sermon_topic->name;
    }
}

New Method that didn't work

// Sermon Topics
$prefix_topic_terms = array();
if ( ! empty( $prefix_topic_terms ) ) {
    $prefix_sermon_topic = get_terms( array(
        'taxonomy' => 'ctc_sermon_topic',
        'hide_empty' => false,
        'orderby' => 'name',
        'hide_empty' => true,
    ));

    foreach ($prefix_sermon_topic as $category) {
        $prefix_topic_terms[$category->slug] = $category->name;
    }
}

var_dump($prefix_topic_terms);

Displays Error Invalid Taxonomy error as below

object(WP_Error)#2577 (2) {
  ["errors"]=>
  array(1) {
    ["invalid_taxonomy"]=>
    array(1) {
      [0]=>
      string(17) "Invalid taxonomy."
     }
  }
  ["error_data"]=>
  array(0) {
  }
}

What am I doing wrong?

Edited Got success with the following code

// Sermon Topics
$prefix_topic_terms = array();
$prefix_sermon_topic = get_terms( array(
    'taxonomy' => 'ctc_sermon_topic',
    'orderby' => 'name',
    'hide_empty' => true,
));
if ( ! empty( $prefix_topic_terms ) && ! is_wp_error( $prefix_topic_terms ) ) {
    foreach($prefix_sermon_topic as $category) {
        $prefix_topic_terms [$category->slug] = $category->name;
    }
}

The above code works if i remove the if condition.

2
  • Are you sure that's what your code looks like? $prefix_sermon_topic holds the get_terms() return value, but you dump $prefix_topic_terms which should be an array. On a side note, you have hide_empty twice in your arguments for get_terms().
    – janh
    Commented Oct 7, 2017 at 10:02
  • even if dump $prefix_sermon_topic it shows invalid taxonomy
    – Maqk
    Commented Oct 7, 2017 at 10:40

2 Answers 2

1

OK, wild guess:

$prefix_topic_terms= array();
if ( ! empty( $iva_topic_terms ) ) {

vs

$prefix_topic_terms = array();
if ( ! empty( $prefix_topic_terms ) ) {

You've changed the variable that is checked for emptiness in the new code. That code will never query get_terms for that taxonomy, because $prefix_topic_terms will not be empty. The Error-object you are receiving is probably unrelated from an earlier call to get_terms.

1
  • I tried that but still no luck. But I'm posting an answer again
    – Maqk
    Commented Oct 7, 2017 at 11:23
0

Got it fixed as I was checking the variables which are empty and defined as an array. Now converted the code into a function. Hope this is helpful for others

Earlier the condition was wrong. Now I am checking whether the passed variable is a WordPress Error with is_wp_error and checking the taxonomy terms variable is not empty.

function sermon_topic() {
    $prefix_terms = array();
    $prefix_postype_taxonomy = get_terms( array(
        'taxonomy' => 'ctc_sermon_topic',
        'orderby' => 'name',
        'hide_empty' => true,
    ));
    if ( ! empty( $prefix_postype_taxonomy ) && ! is_wp_error( $prefix_postype_taxonomy ) ) {
        foreach($prefix_postype_taxonomy as $category) {
            $prefix_terms[$category->slug] = $category->name;
        }
    }
    return $prefix_terms;
}

Where I want to pull the terms I use function sermon_topic();

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.