Using WordPress ‘register_{$post_type}_post_type_args’ PHP filter

The “register_{$post_type}_post_type_args” dynamic filter allows you to modify the arguments for registering a specific post type in WordPress.

Usage

To use this filter, create a custom function and hook it to the dynamic filter. The filter name depends on the post type being registered.

add_filter("register_{$post_type}_post_type_args", 'your_custom_function', 10, 2);

function your_custom_function($args, $post_type) {
    // Your modifications to $args go here
    return $args;
}

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 ‘has_archive’ property for a custom post type

add_filter('register_books_post_type_args', 'change_books_archive', 10, 2);

function change_books_archive($args, $post_type) {
    $args['has_archive'] = false;
    return $args;
}

This code sets the has_archive property to false for the ‘books’ post type, disabling the archive page for it.

Make a custom post type hierarchical

add_filter('register_projects_post_type_args', 'make_projects_hierarchical', 10, 2);

function make_projects_hierarchical($args, $post_type) {
    $args['hierarchical'] = true;
    return $args;
}

This code sets the hierarchical property to true for the ‘projects’ post type, allowing parent-child relationships between items.

Change the ‘supports’ property for a custom post type

add_filter('register_events_post_type_args', 'modify_events_supports', 10, 2);

function modify_events_supports($args, $post_type) {
    $args['supports'] = array('title', 'editor', 'thumbnail', 'custom-fields');
    return $args;
}

This code changes the supports property for the ‘events’ post type, adding support for custom fields.

Add a custom rewrite slug for a custom post type

add_filter('register_portfolio_post_type_args', 'change_portfolio_rewrite', 10, 2);

function change_portfolio_rewrite($args, $post_type) {
    $args['rewrite'] = array('slug' => 'projects');
    return $args;
}

This code sets the rewrite slug for the ‘portfolio’ post type to ‘projects’, changing the URL structure.

Change the ‘capability_type’ property for a custom post type

add_filter('register_members_post_type_args', 'change_members_capability', 10, 2);

function change_members_capability($args, $post_type) {
    $args['capability_type'] = 'member';
    return $args;
}

This code sets the capability_type property to ‘member’ for the ‘members’ post type, allowing for custom capabilities.