Skip to content

Instantly share code, notes, and snippets.

@jonshipman
Created October 27, 2023 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonshipman/4678f3ce51637097a7ca5f73883c4613 to your computer and use it in GitHub Desktop.
Save jonshipman/4678f3ce51637097a7ca5f73883c4613 to your computer and use it in GitHub Desktop.
custom_post_type_boilerplate.php
<?php
/**
* Your custom post type
*
* @package Pacakge_Name
*/
/**
* Post Type.
*/
function prefix_cpt_yourcustomposttype() {
$args = array(
'label' => __( 'Your Custom Post Type', 'textdomain' ),
'description' => '',
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_rest' => true,
'rest_base' => '',
'rest_controller_class' => 'WP_REST_Posts_Controller',
'has_archive' => false,
'show_in_menu' => true,
'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,
'menu_icon' => 'dashicons-generic',
'supports' => array( 'title', 'editor', 'custom-fields' ),
);
register_post_type( 'your_custom_post_type', $args );
}
add_action( 'init', 'prefix_cpt_yourcustomposttype' );
/**
* Register register custom meta for your custom post type.
*/
function prefix_cpt_yourcustomposttype_rest() {
register_post_meta(
'your_custom_post_type',
'some_meta_value',
array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'auth_callback' => '__return_true',
)
);
}
add_action( 'init', 'prefix_cpt_yourcustomposttype_rest', 9 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment