How often do you need to register_post_type?

Assuming you write a plugin and hook into activation / deactivation to register_post_type is that good enough? Or do you need to do it every init?

I’m looking for perf boosts and I want to reduce unnecessary calls.

GenerateWP uses add_action( 'init', 'custom_post_type', 0 ); so that might be as early as I might want to register it.

FINDINGS

  • @s-ha-dum:

    register_post_type needs to be called on every page load

    The post data itself is kept in the database, but the registration tells the PHP what to do with it.

  • @pieter-goosen:

    Build in types are actually registered twice on every page load (due to localization that is only available on init)

2 Answers
2

register_post_type needs to be called on every page load– init seems to be fine and is the hook used in the Codex sample. The post data itself is kept in the database, but the registration tells the PHP what to do with it. Most of the post type information– the $labels, the $args— are not kept in the database to my knowledge (though I would agree that there might be an argument for doing so), so without that registration code Core doesn’t really know about the post type.

You can pretty easily test this yourself by registering the type and then commenting the code.

Leave a Comment