Skip to content

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.

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.

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

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

<?php
/**
 * Filter the Products CPT to register more options.
 *
 * @param $args       array    The original CPT args.
 * @param $post_type  string   The CPT slug.
 *
 * @return array
 */
function grd_client_filter_products_cpt( $args, $post_type ) {
	// If not Products CPT, bail.
	if ( 'products' !== $post_type ) {
		return $args;
	}
	// Add additional Products CPT options.
	$products_args = array(
		'has_archive' => true,
		'supports'    => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields', 'page-attributes' ),
	);
	// Merge args together.
	return array_merge( $args, $product_args );
}
add_filter( 'register_post_type_args', 'grd_client_filter_products_cpt', 10, 2 );

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

About Greg Rickaby

Director of Engineering @WebDevStudios / Author & Tech Editor @ForDummies / @WordPress Contributor / @gatsbyjs Maintainer / Follow Greg on Twitter

5 Comments

  1. Alvin on September 21, 2016 at 08:00

    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!

  2. Felipe Romero on June 18, 2017 at 19:24

    Thoroughly explained, thank you very much. This worked wonders for me!

  3. Mohammed on July 4, 2017 at 06:33

    Thanks a lot.. Working so nice!

  4. karl632 on September 7, 2017 at 05:07

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

  5. Chet on August 28, 2019 at 14:58

    Thank you!

Leave a Comment