Altering Registered Post Type Args

Today I was working on a project where I needed to change how a post type had been registered. The post type in question was setting public to false, so the post type was not a public post type. However I was utilising the post type somewhere else and needed to change this. Here is how I went about it.

Obviously when doing this I did not want to change any core or plugin code. The plugin creating the post type was the excellent whistles plugin from Justin Tadlock. This is a plugin that enables you to add snippets of content here and there including adding stuff in accordions and tabs etc.

I wanted to to use the post type as a featured content slider with the Soliloquy slider plugin. However the problem was that the post type did not exist in the drop down to select when creating a new slider. Having looked at the way in which the Whistles plugin registered its post type, it was clear that the reason it wasn’t showing as a post type to choose in the slider was because the post types ‘public’ arg was set to false. Therefore I set about finding a way to change this without chaining the plugin itself.

The first thing I did was to find in the plugin files where the post type was registered, hoping for a hook or filter which I could use in order to change the args when registering the post type. However in inspection it became clear nothing was present. This left me looking in core.

I found the file containing the function register_post_type() and noticed at the bottom of this function is a cal to do action named registered_post_type. Passed to this action is the post type name and the args for registering that post type. This meant that I could use this action in order to change any of the registered post type args.

Below is the code I used in order to change the public arg from false to true.

[wpmark_gist id=”86a965d71a6a7333ddd9″]

You can change any of the args that are registered. A dump of the ‘args’ from that function shows the following:

[wpmark_gist id=”292d91a50c6c9bafa009″]

Therefore any of these can be changed in a similar way.

4 responses

  1. Exactly the code I was looking for. A support thread on wordpress.org showed code that changed the post type args and the reregistered it.
    https://wordpress.org/support/topic/modifying-post-type-using-the-registered_post_type-hook?replies=5

    With your code I was able to hide a number of CPTs provided by a premium theme and the theme didn’t have hooks or actions that I could interact with.
    As I was hiding a number of them I used in_array():

    if (!in_array($post_type, array(‘audio’, ‘event’, ‘portfolio’, ‘video’))) {
    return;
    }

  2. Awesome, thanks.

  3. The code isn’t showing any longer on your post.. Would love to see it!

Leave a Reply

Your email address will not be published. Required fields are marked *