2

I don't know why get_term() works on other pages but won't work in functions.php. Using get_term() in functions.php causes WordPress to report the error Invalid taxonomy.

my function.php which is ajax handler and already registered for ajax

 public function load_destination()
    {
        global $wpdb;
        $termId = $_POST['termid']; 
       $term =  get_term($termid,'package_state');
       $args = array(
'post_type' => 'package',
'tax_query' => array(
    array(
    'taxonomy' => 'package_state',
    'field' => 'id',
    'terms' => $termId
     )
  )
);

$query = new WP_Query( $args );
$collection=[];
$count =1;
while($query->have_posts()) : $query->the_post();

$collection['data'][] = // i need to set term data here $term;


$count++;
endwhile;
    wp_send_json($collection);    
    }
3

It sounds like you are are using get_term() for a custom taxonomy. If you're using get_term() inside functions.php and outside of a hooked function, that code is going to be run immediately when functions.php is loaded. Your custom taxonomy will not have been registered at this point, because that happens on the init hook.

Your code is working inside of your page template because WordPress has loaded the custom taxonomies at that point.

If you were to try something like $term = get_term( '2', 'category' ); (where 2 is a valid term ID) in functions.php, it would actually work, because WordPress loads built-in taxonomies in wp-settings.php (which is very early in the loading process) for backwards compatibility reasons. WordPress also registers default taxonomies on init, by the way. This is explained in the documentation block for create_initial_taxonomies() in /wp-includes/taxonomy.php.

Anyway, running get_term() outside of a hooked function in functions.php is not the right way to do it, and more code would be needed to help you further.

3
  • so how could i get this task done as i was working on ajax so i need to load an term object and show but couldnt load it damn me Sep 11 '16 at 6:06
  • 2
    Without showing your code, I don't know. This answer already required the limits of my mind reading capabilities ;-) Sep 11 '16 at 6:09
  • i have updated my code Sep 12 '16 at 14:38
0

https://codex.wordpress.org/Function_Reference/get_term

Uses global: (object) $wpdb

try declaring

global $wpdb;

before your get_term() query?

It may work, depending on the code you're trying to use.

5
  • no its aint working still getting invalid taxonomy but same stuff works over other pages except functions.php Sep 11 '16 at 4:38
  • 1
    You'll have to post the code you're trying to working with. Edit your original post
    – bbruman
    Sep 11 '16 at 5:05
  • the code is same get_term() but the use of code is different one is functions.php and other is page.php Sep 11 '16 at 5:26
  • 1
    Post the code from your functions.php. Sep 11 '16 at 5:51
  • $test = get_term(8,'package_state'); var_dump($test) Sep 11 '16 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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