Custom Post Type for single post
-
Is it able to create a custom post type that only has single post(Not allow to add more posts). I would like to make a page to store the layout configuration. Then I can retrieve the setting from that single post. Is it possible to do that?
-
I don’t see why it wouldn’t be, but in terms of going “the extra mile” to prevent the potential of someone adding more posts in the post type, you’d need to get a little creative. Either tinker with roles/capabilities to prevent access to all but specific people, or perhaps simply hide the UI conditionally. If you’re going for the “bare bones”, just publish the post, set up how you need for this layout config, and then just don’t publish any more. Fetching/retrieving the info for the config could be done using the
get_post()
and potentiallyget_post_meta()
function alongside passing in the post’s ID for them.Everything is fine and working. The last issue is disabling from adding new post. To achieve, I found this solution on Stackoverflow.
Here is the link: https://stackoverflow.com/questions/3235257/wordpress-disable-add-new-on-custom-post-type
How do I modify with CPT UI?
Thank you very much
@viswong looking at that stackoverflow link, it’s delving into the topic of capabilities modification. For that, since we know and acknowledge that we don’t have 100% coverage of everything, we’ll need to delve into using some custom code and CPTUI’s filters. Alternately, if you’re willing to try an extension I have I can link you to a secondary plugin that would add some extra details to your post type screen. Let me know which method you’d prefer. I can help with both.
Thank you for your help @tw2113. I can do it in anyway, maybe the secondary plugin is more easy to me.
Thanks again
Gonna have to go the first way, because this edit dips into a deeper spot than my extension covers.
So. We have the following. It covers two possible cases. If you want this inability to edit to apply to only one post type created, we use the first example. If you want to have this inability to apply to all of them from CPTUI, use the second example. If you’re only going to have 1 post type created, just go with the second one.
What we’re basically doing is intercepting the arguments that CPTUI uses, right before we do the registration, and add in a couple more details programmatically. One last failsafe check that we added many moons ago.
function viswong_custom_capabilities( $args, $post_type_name ) { // If you only want this to apply to certain post types. // Change "SOMETHING" to the actual slug used. if ( 'SOMETHING' === $post_type_name ) { $args['capabilities'] = array( 'create_posts' => 'do_not_allow', ); return $args; } } add_filter( 'cptui_pre_register_post_type', 'viswong_custom_capabilities', 10, 2 );
function viswong_custom_capabilities( $args, $post_type_name ) { // If you want to apply to all of them. $args['capabilities'] = array( 'create_posts' => 'do_not_allow', ); return $args; } add_filter( 'cptui_pre_register_post_type', 'viswong_custom_capabilities', 10, 2 );
Either of these should go in your active theme’s functions.php or if things are ideal, you have a child theme that you can drop these in.
Welcome.
@tw2113 Sorry, Michael… I found that the codes hide my other custom post type. I don’ t want to affect it capability. What should I do? Thank you very much!
If you used the last example I provided, change things up to this one. Remember to change out the post type slug values. The “SOMETHING” below. Sounds like you used the snippet that was going to affect all of them.
function viswong_custom_capabilities( $args, $post_type_name ) { // If you only want this to apply to certain post types. // Change "SOMETHING" to the actual slug used. if ( 'SOMETHING' === $post_type_name ) { $args['capabilities'] = array( 'create_posts' => 'do_not_allow', ); return $args; } } add_filter( 'cptui_pre_register_post_type', 'viswong_custom_capabilities', 10, 2 );
- This reply was modified 5 years, 10 months ago by Michael Beckwith.
It is weird… I did use the condition of “if ( ‘TARGET_CPT_SLUG’ === $post_type_name )” that is work, it is unable to add more post but also hide my another one CPT on the dashboard…
hrmm. Last idea I have for what’s going on, we need to move that
return $args
line. Something like this, for a “complete” visual of the code.function viswong_custom_capabilities( $args, $post_type_name ) { // If you only want this to apply to certain post types. // Change "SOMETHING" to the actual slug used. if ( 'SOMETHING' === $post_type_name ) { $args['capabilities'] = array( 'create_posts' => 'do_not_allow', ); } return $args; } add_filter( 'cptui_pre_register_post_type', 'viswong_custom_capabilities', 10, 2 );
- This reply was modified 5 years, 10 months ago by Michael Beckwith.
Its really work this time! Thank you very much!
- The topic ‘Custom Post Type for single post’ is closed to new replies.