Greg Rickaby

Greg Rickaby

Cameras and Code

Filter and modify a Custom Post Type – after it’s been registered

Today I was tasked with adding functionality to an existing custom post type. The problem was, the CPT was being registered in a plugin which I could not edit (client declined to give WDS write access to the /plugins folder).

After some digging, I found there’s a filter for that (found in post.php) which allows you to modify a custom post type (after it’s been registered some other way) and could do so in the theme.

apply_filters( 'register_post_type_args', $args, $post_type );

Hooking into that filter, allowed me to pass additional options to an existing CPT:

https://gist.github.com/gregrickaby/518090ce734dd98842adef6f82c597d0

Now “Products” has an archive, along with some other meta boxes in the post editor. Nice!

Category: Code

Tags: snippets

Comments

Alvin

Nice post and exactly what i was looking for! I have the same problem: a CPT registered by a plugin doesnt have tags, categories and a text field. I dont want a developer to modify the plugin because its constantly updating. Now i see that this would rather be a “simple” addition to the (child) theme. Thx!

karl632

Thanks, just what I needed to disable has_archive for a CPT added by the parent theme.

ASD FGEE

Thanks, just what I needed to disable has_archive for a CPT added by the parent theme.