Problems with function on function.php

I’ve a problem with some functions on function.php. I want to use get_tems_by(“slug”, $slug, “category”); but it doesn’t work inside a function on function.php. When i changed slug by ID and give a random ID it’s work.
I’m sure that the slug exist.
I’ve also try that :

add_action( 'init', 'wpse27111_tester', 999 );
function wpse27111_tester()
{
    $term = get_term_by('slug', 'some-term', 'some-taxonomy');
    var_dump($term);
}

And it’s work but i need to put $slug

If you have a solution please tell me.

2 Answers
2

Did you try to use add_action without priority? On the last line, you specify the priority. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.

function test_1234567() {
  // Get term by name ''news'' in Categories taxonomy.
  $category = get_term_by('name', 'news', 'category');

  // Get term by name ''news'' in Tags taxonomy.
  $tag = get_term_by('name', 'news', 'post_tag');

  // Get term by name ''news'' in Custom taxonomy.
  $term = get_term_by('name', 'news', 'my_custom_taxonomy');

  // Get term by name ''Default Menu'' from theme's nav menus.
  // (Alternative to using wp_get_nav_menu_items)
  $menu = get_term_by('name', 'Default Menu', 'nav_menu');

  var_dump($category);
}

add_action( 'init', 'test_1234567' );

Also, you do not need to specify the priority, the default is 10.

Leave a Comment