There are lots of situations where a theme or plugin registers a post type and you want to modify it. There is of course add_post_type_support()
and remove_post_type_support()
, but those don’t give access to the full list of arguments that register_post_type()
takes. In particular, maybe I want to disable a post type archive, hide the admin UI, hide from search, etc. while leaving the rest of the post type settings alone.
The Codex page for register_post_type()
dangles this in front of me:
Description
Create or modify a post type.
But in the past, when I’ve try to do this, it hasn’t seemed to work. Is this function really for modifying post types, and if so, can you simply redeclare a couple arguments and leave the rest alone?
Seeing that there isn’t even a deregister_post_type()
function, I don’t understand how it can be done.
Yes.
No. If you want to modify arguments to a post type, you need to use
get_post_type_object
to get the post type object, modify what you want in it, then re-register it using your modified type as the new $args parameter.After some research I found none of these answers are up to date.
As of December 8, 2015 WordPress includes a new filter,
register_post_type_args
, which lets you hook into the arguments of a registered post type.Here’s an example of how to use the
'registered_post_type'
filter to modify a post type in another plugin.A plugin I was using didn’t include a menu_icon in it’s definition, so I wanted to add one of my own.
Hook into
'registered_post_type'
after the other code has registered it. It is called at the end ofregister_post_type()
. You get two arguments:$post_type
and$args
.Now you can change anything for this post type. Inspect
$GLOBALS['wp_post_types']
for some options.I faced the same thing with The Events Calendar plugin.
I added the below code to function.php to modify the tribe_organizer post type
I dont know if this is ugly, but you could alter the
GLOBAL
placeholder “on the fly” whenever you need to manipulate a single argument. This is how we use a non-public post typecontents
to be acceptable in admin menu. We hook close before and close after the menu renders:In our case, we want the Admin Menu Post List plugin to accept our post type, as they call
return get_post_types(array('public' => true));
inside their hook with priority 11 …Developers -Please make a comment if this could couse any issues.