Don’t register custom post types before ‘init’ fires

I encountered this error while building a custom post type and taxonomy:

Fatal error: Call to a member function add_rewrite_tag() on a non-object

I’d written lots of custom CPTs before using the same syntax without any issues. The problem ended up being calling these functions too early in WordPress’s execution. In my case, I had called register_post_type() in my custom class’s __construct() method. WordPress hadn’t setup all the structure necessary to register a new post type, register_post_type() and register_taxonomy() need to be called on (or after) the init hook.

Quick Summary

Cause: calling register_taxonomy() or register_post_type() too early in the WP execution.

In my case: I called register_post_type() in a custom plugin within the __construct()

Solution: place the register_post_type() call within an action that fires on (or after) the init hook

Comments are closed, but trackbacks and pingbacks are open.