Uncaught Error: Call to a member function add_rewrite_tag() on null

By On

I am developing a custom plugin in wordpress and i am trying to get a new post type added. I have followed some guides to get this done and I dont see any problem with what I have done. The syntax is good and there is no php error being thrown. Still I get the error 'Uncaught Error: Call to a member function add_rewrite_tag() on null' whenever I load a page on the site. What causes this to happen and how do i fix it?

1 Answer

- Advertisment -
SEMrush
Answered By Anthony Fisher On

To give a more technical explanation first, this method is in fact a php error. It is telling you that the function called ""add_reqrite_tag"" does not exist. Since we know this is a valid function in the wordpress framework, we know that this error is wrong, but it is reporting the error in the correct way. What is actually happening, is that you are trying to call this function before the wordpress framework has loaded fully. You need to make sure that you are declaring your new post type within a wordpress hook. This way it will only be called once the framework has loaded and this method exists. 

Normally post types are declared inside the init hook. Check your code and make sure register_post_type isn't being called outside of the init or whatever other hook you indend on using to declare the post type. Init is where it should go btw. "