Using WordPress ‘register_post_type_args’ PHP filter

 ‘register_post_type_args’ is a WordPress PHP filter that allows you to modify the arguments used when registering a custom post type. This can be helpful for customizing various aspects of your post type without directly modifying its registration code.

Usage

To use this filter, add your own custom function to your theme or plugin, then hook it to the register_post_type_args filter. In your custom function, you can modify the $args array and return it.

Code Example

function my_custom_post_type_args( $args, $post_type ) {
    // Modify $args as needed
    return $args;
}
add_filter( 'register_post_type_args', 'my_custom_post_type_args', 10, 2 );

Parameters

  • $args (array)
    • Array of arguments for registering a post type.
    • See the register_post_type() function for accepted arguments.
  • $post_type (string)
    • Post type key.

Examples

Change the default supports value

function change_default_supports( $args, $post_type ) {
    // Set 'supports' to a custom set of values
    $args['supports'] = array( 'title', 'editor', 'thumbnail', 'custom-fields' );
    return $args;
}
add_filter( 'register_post_type_args', 'change_default_supports', 10, 2 );

In this example, we change the default supports value for all post types to include title, editor, thumbnail, and custom fields.

Set custom post type as hierarchical

function set_custom_post_type_as_hierarchical( $args, $post_type ) {
    if ( 'my_custom_post_type' === $post_type ) {
        $args['hierarchical'] = true;
    }
    return $args;
}
add_filter( 'register_post_type_args', 'set_custom_post_type_as_hierarchical', 10, 2 );

In this scenario, we set the ‘my_custom_post_type’ post type as hierarchical.

Modify the rewrite rules for a custom post type

function modify_rewrite_rules( $args, $post_type ) {
    if ( 'my_custom_post_type' === $post_type ) {
        $args['rewrite'] = array( 'slug' => 'custom-slug', 'with_front' => false );
    }
    return $args;
}
add_filter( 'register_post_type_args', 'modify_rewrite_rules', 10, 2 );

Here, we modify the rewrite rules for the ‘my_custom_post_type’ post type, changing the slug and setting with_front to false.

Set a custom post type as publicly queryable

function set_custom_post_type_publicly_queryable( $args, $post_type ) {
    if ( 'my_custom_post_type' === $post_type ) {
        $args['publicly_queryable'] = true;
    }
    return $args;
}
add_filter( 'register_post_type_args', 'set_custom_post_type_publicly_queryable', 10, 2 );

In this example, we set the ‘my_custom_post_type’ post type as publicly queryable, making it accessible on the front-end.

Change the default menu_position for a custom post type

function change_default_menu_position( $args, $post_type ) {
    if ( 'my_custom_post_type' === $post_type ) { 
       $args['menu_position'] = 25; 
    } 
    return $args; 
} 
add_filter( 'register_post_type_args', 'change_default_menu_position', 10, 2 );

In this example, we change the default `menu_position` for the ‘my_custom_post_type’ post type to 25, adjusting its location in the WordPress admin menu.