1

I just want to know why get_object_taxonomies('custom-post-type-name') return empty array for custom post types but works fine with post as argument : get_object_taxonomies('post') and returns:

array (size=3)
  0 => string 'category' (length=8)
  1 => string 'post_tag' (length=8)
  2 => string 'post_format' (length=11)
5
  • Are you sure the problem is not a simple typo?
    – Johansson
    Commented Feb 19, 2018 at 20:36
  • Where are you using this code? Perhaps you're outside of the Loop so there is no object it's running on?
    – WebElaine
    Commented Feb 19, 2018 at 21:42
  • @JackJohansson I found the cause of this issue : the problem is that the post types isn't recognizable inside my plugin folder :/any idea ?
    – geeky87
    Commented Feb 20, 2018 at 12:17
  • @WebElaine : the problem is that the post types isn't recognizable inside my plugin folder
    – geeky87
    Commented Feb 20, 2018 at 12:18
  • As you mentioned the function works fine with an argument - have you tried passing one of the CPTs as an argument to see if that works? Hard to help without further context about where and how this code is running.
    – WebElaine
    Commented Feb 20, 2018 at 18:27

1 Answer 1

1

I have run into the same issue. I believe the root cause is the order of execution. For the sake of experiment, I ran get_object_taxonomies() immediately after register_taxonomy(), in fact in the same function and it worked as expected. So what to do? Use add_action() hook to execute get_object_taxonomies() after register_taxonomy(). The correct order of execution can be guaranteed via 3rd argument of add_action( ... int $priority = 10 ... )

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.