2

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

1

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.

2
  • Default is priority 10
    – David
    Oct 12, 2018 at 16:27
  • Oops, my bad. You're right David Oct 13, 2018 at 7:15
0

Try something like this:

add_action( 'init', 'wpse316530_func' );
function wpse316530_func()
{
    $slug = 'uncategorized';
    $term = get_term_by('slug', $slug, 'category');
    var_dump($term);
}

What I did was to remove the priority argument from add_action function, as you don't really need it.

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.