I have this code that is supposed to limit the user to the edit of (only) one predefined created custom post type. I want to setup only one post that is responsable to manage the homepage contents. I have an issue, I've noticed that the post type is ceated more than one time, I need that the user can edit only a single post so I've implemeted this:
function edit_homepage_link()
{
global $submenu, $pagenow;
// query the homepage posts
$homepage = new WP_Query( array( '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'];
}
add_action( 'admin_menu', 'edit_homepage_link' );
function register_homepage_cpt() {
$labels = array(
'name' => _x( 'Homepage', 'post type general name'),
'singular_name' => _x( 'Homepage', 'post type singular name'),
'menu_name' => _x( 'Homepage', 'admin menu'),
'name_admin_bar' => _x( 'Homepage', 'add new on admin bar'),
'featured_image' => __( 'Featured Image', 'text_domain' ),
);
$args = array(
'labels' => $labels,
'description' => __( 'Personalizza Homepage', '' ),
'public' => true,
'show_in_menu' => true,
'show_ui' => true,
'menu_position' => null,
'capability_type' => 'page',
'supports' => array('title','editor','thumbnail','custom-fields','excerpt')
);
$post_data = array(
'post_title' => 'Home Page',
'post_type' => 'homepage',
'post_statue' => 'publish',
'post_author' => 1
);
wp_insert_post( $post_data );
register_post_type( 'homepage', $args );
}
add_action( 'init', 'register_homepage_cpt' );
How I can fix my issue, is there a way to register and programmatically create only one time a post? Help please. The original code solution is taken from an old answer that I've found here.