Richnou
Richnou

Reputation: 126

Create a hidden custom post type in wordpress - but not totally

I need to register a custom post type, said "hidden_cpt", which to be :

I've try to do some test with register_post_type args, but it's either too hidden or not enough ...

Do you have an idea ?

Thank's in advance

EDIT: I tried around the following code, changing some "true" to "false" and vice versa... but I did not keep all my tests :-( This one is not the best one...

function register_hidden_cpt() {
    $labels = [
        "name" => __("HCPT", "hidden_cpt"),
        "singular_name" => __("HCPT", "hidden_cpt"),
        "menu_name" => __("HCPT", "hidden_cpt"),
    ];
    $args = [
        "label" => __("HCPT", "hidden_cpt"),
        "labels" => $labels,
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => true,
        "rest_base" => "",
        "rest_controller_class" => "WP_REST_Posts_Controller",
        "has_archive" => true,
        "show_in_menu" => true, 
        "show_in_nav_menus" => true,
        "delete_with_user" => false,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => false,
        "query_var" => true,
        "supports" => ["title", "editor", "thumbnail"],
    ];
    register_post_type("hidden_cpt", $args);
}
add_action('init', 'register_hidden_cpt');

Upvotes: 0

Views: 1864

Answers (1)

Aliqua
Aliqua

Reputation: 748

function register_hidden_cpt() {
    $labels = [
        "name" => __("HCPT", "hidden_cpt"),
        "singular_name" => __("HCPT", "hidden_cpt"),
        "menu_name" => __("HCPT", "hidden_cpt"),
    ];
    $args = [
        "label" => __("HCPT", "hidden_cpt"),
        "labels" => $labels,
        "public" => true, // for acf
        "publicly_queryable" => true,
        "show_ui" => false,
        "show_in_rest" => false,
        "rest_controller_class" => "WP_REST_Posts_Controller",
        "has_archive" => true,
        "show_in_menu" => false, 
        "show_in_nav_menus" => false,
        "delete_with_user" => false,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => false,
        "query_var" => true,
        "supports" => ["title", "editor", "thumbnail"],
    ];
    register_post_type("hidden_cpt", $args);
}
add_action('init', 'register_hidden_cpt');

Refer to https://developer.wordpress.org/reference/functions/register_post_type/

Some array values are 'parents' to other values which if the parents are false, you dont need to include the child (unless you want the child argument to override the defaults obviously). Have a good look at the reference page above, read the descriptions as to what they do, and you can clean the code even more to your requirements. Almost anything is available through wp_query, but i dont have access to acf so infant provide any insight into that request.

Hopefully this has helped. Good luck!

Update

That would be a good idea to ask on ACF forums. You could also do a conditional in any additional function you write or make it hidden for anyone other than administrators? eg, make it public for acf, but then hide it in any function you write? I found this example on stackexchange - https://wordpress.stackexchange.com/questions/28782/possible-to-hide-custom-post-type-ui-menu-from-specific-user-roles

Credit: @Milo (StackExchange)

To hide a post type menu item from non-admin users:

function wpse28782_remove_menu_items() {
    if( !current_user_can( 'administrator' ) ):
        remove_menu_page( 'edit.php?post_type=your_post_type' );
    endif;
}
add_action( 'admin_menu', 'wpse28782_remove_menu_items' );

your_post_type should be the name of your actual post type.

Upvotes: 1

Related Questions