Modify Arguments of Already Registered Custom Post Type with Filter

Viewed 340

Im trying to set the show_in_rest argument on a custom post type that was already registered by a third party plugin from false to true. The plugin author thankfully included an arguments filter and provided an example (see below first code block). Any idea how to set the mentioned argument to true?

This is the CPT register function

    public function register_client() {

    $labels = apply_filters( 'business_manager_client_labels', array(
        'name'                  => _x( '%2$s', 'post type general name', 'business-manager' ),
        'singular_name'         => _x( '%1$s', 'post type singular name', 'business-manager' ),
        'add_new'               => __( 'New %1s', 'business-manager' ),
        'add_new_item'          => __( 'Add New %1$s', 'business-manager' ),
        'edit_item'             => __( 'Edit %1$s', 'business-manager' ),
        'new_item'              => __( 'New %1$s', 'business-manager' ),
        'all_items'             => __( '%2$s', 'business-manager' ),
        'view_item'             => __( 'View %1$s', 'business-manager' ),
        'search_items'          => __( 'Search %2$s', 'business-manager' ),
        'not_found'             => __( 'No %2$s found', 'business-manager' ),
        'not_found_in_trash'    => __( 'No %2$s found in Trash', 'business-manager' ),
        'parent_item_colon'     => '',
        'menu_name'             => _x( '%2$s', 'admin menu', 'business-manager' ),
        'filter_items_list'     => __( 'Filter %2$s list', 'business-manager' ),
        'items_list_navigation' => __( '%2$s list navigation', 'business-manager' ),
        'items_list'            => __( '%2$s list', 'business-manager' ),
    ) );

    foreach ( $labels as $key => $value ) {
        $labels[ $key ] = sprintf( $value, business_manager_label_client_single(), business_manager_label_client_plural() );
    }

    $args = array(
        'labels'                => $labels,
        'public'                => false,
        'show_in_rest'          => false,
        'exclude_from_search'   => true,
        'publicly_queryable'    => false,
        'show_ui'               => true,
        'show_in_menu'          => false, // we are using custom add_submenu_page
        'query_var'             => true,
        'capability_type'       => 'post',
        'map_meta_cap'          => true,
        'has_archive'           => false,
        'hierarchical'          => false,
        'supports'              => array( 'title' ),
    );

    register_post_type( 'bm-client', apply_filters( 'business_manager_client_post_type_args', $args ) );

}

This is the provided example

add_filter( 'business_manager_employee_post_type_args', 'example_function_for_employee_post_type', 10, 1 );

function example_function_for_employee_post_type( $args ) {
    print( $args ); //print the return value
    return $args;
}
2 Answers

Try the below code. you can access "show_in_rest" key in "$args" parameter and update the value by true.

add_filter( 'business_manager_employee_post_type_args', 'example_function_for_employee_post_type', 10, 1 );
function example_function_for_employee_post_type( $args ) {
    $args['show_in_rest'] = true;
    return $args;
}

For anyone looking to change this in any other CPT that does not have a predefined filter hook. CTP registration function already contains the required filter hook that may be used: register_post_type_args

So this becomes:

add_action('register_post_type_args', function ($args, $postType) {
    if ($postType !== 'bm-client'){
        return $args;
    }

    $args['show_in_rest'] = true;

    return $args;
}, 99, 2);

Related article: https://zerowp.com/modify-cpt-arguments-after-it-was-registered/

Related