how to only have one custom post type post?

Is it possible to limit a CPT to just one? What I’d like to accomplish is creating a CPT called “Home”. It will manage any and all elements on the home page. I want to program it so when the user clicks on the “Manage Home Page” link they will go straight to the edit post screen. They will skip over the “All Post” screen. Does anyone think this is even possible?

Or maybe someone has an idea to accomplish this goal a completely different?

Related posts

Leave a Reply

5 comments

  1. You want this because your client is confused, and setting a page to the homepage will not do.

    But your solution is a can of worms waiting to explode in your face, so instead I have a much better solution!

    Use the home.php template

    In WordPress by default home.php is used as the homepage. If it is not found, front-page.php is used, and if that isn’t found it uses index.php

    So create home.php, put your homepage code in there, and then add a settings page with WYSIWYG editors and image uploaders for the various pieces of content. You can even link directly to the settings page from the frontend if logged in to the site.

    If your client is still confused, use screensteps to show them how. Chances are your client is only confused because they haven’t invested any time to read instructions or figure it out.

    The added bonus of this is it doesn’t leave any confusing steps like clicking on the listings and finding a single post available, and an ‘add new home page’ button that does nothing but generate warnings and permission denied messages.

  2. Steve, my understanding is that you want:

    1. a custom post type with no All Posts sub menu
    2. you just want ‘Edit Home Page’ link to appear under Posts submenu?
    3. And you don’t want your users to add more then 1 post for that particular post type.

    The code below will tweak the WordPress Admin to do the above:

    Note users can still add the post to custom post type using wp_insert_post

    <?php
    /**
     * Plugin Name: Home Page CPT
     **/
    
    class WPSE_26330_Homepage_CPT {
    
        function __construct() {
    
            // add the default homepage on plugin activation
            register_activation_hook( __FILE__, array( &$this, 'add_home_page_post' ) );
    
            // register the homepage post type
            add_action( 'init', array( &$this, 'register_homepage_cpt' ) );
    
            // add the menu link
            add_action( 'admin_menu', array( &$this, 'edit_homepage_link' ) );
        }
    
        function edit_homepage_link() {
            global $submenu, $pagenow;
    
            // query the homepage posts
            $homepage = new WP_Query( 'post_type=homepage' );
    
            // if its new post page and we have homepage
            if ( $pagenow == 'post-new.php' && $homepage->have_posts() ) {
                wp_die('You cant add more then one homepage');
            }
    
            // if we have homepage post, show the edit link else the add homepage link
            if ( $homepage->have_posts() ) {
                $homepage->the_post();
                $link = get_edit_post_link( get_the_ID(), 'return' );
                $title = 'Edit Home Page';
            } else {
                // in case if the user has deleted the default post
                $link = get_bloginfo( 'url' ). '/wp-admin/post-new.php?post_type=homepage';
                $title = 'Add Home Page';
            }
            $submenu['edit.php'] = array( array( $title, 'manage_options', $link ) ) + $submenu['edit.php'];
        }
    
        function register_homepage_cpt() {
            $args = array( 
                'label' => 'homepage',
                'description' => 'Home Page post type',
                'public' => true,
                'show_in_menu' => false
            );
            register_post_type( 'homepage', $args );
        }
    
        function add_home_page_post() {
            // on activation first regsiter the post type
            $this->register_homepage_cpt();
    
            // add the first and only post
            $post_data = array(
                'post_title' => 'Home Page',
                'post_type' => 'homepage',
                'post_statue' => 'publish',
                'post_author' => 1
            );
            wp_insert_post( $post_data );
        }
    
    }
    
    $GLOBALS['wpse_homepage_cpt'] = new WPSE_26330_Homepage_CPT;
    ?>
    
  3. I’ve actually done something very similar to this. I used a few different things in conjunction, and it all worked very well for my (easily confused) client.

    Basically, I started with a page called “home”. Then, using Advanced Custom Fields, I created the various fields I wanted the client to be able to control – two WYSIWYG fields, several image fields, a few link fields, you get the idea. Then, when setting up the rules for that ACF type, I hid the main ‘content’ area on the edit page. I made sure to add clear and concise rules for each content area to avoid confusion, so they would know exactly what they could and couldn’t do in each field.

    So, when the client clicks “edit page” in the WordPress menu, they’re presented with the Edit Post page for “Home”, with all the areas they control laid out neatly.

    Then, I set up the home.php template to call the field output in the proper places. There’s several nice tutorials and code snippes on the ACF main site.

    It made a VERY happy client, and took a lot of future frustrating maintenance off my plate.