Using WordPress ‘create_initial_post_types()’ PHP function

The create_initial_post_types() WordPress PHP function is used to establish the initial post types when the ‘init’ action is triggered.



This function does not require any parameters, and it is generally not used in theme or plugin development, as WordPress itself triggers it during the initial setup.


  • This function does not have any parameters.

More information

See WordPress Developer Resources: create_initial_post_types()

This function is internally used by WordPress and is not recommended to be used directly in your code.


Register a new ‘Books’ post type

// Add action hook for 'init'
add_action('init', 'create_books_post_type');

// Function to register new post type
function create_books_post_type() {
    // Options
      'labels' => array(
        'name' => __('Books'),
        'singular_name' => __('Book')
      'public' => true,
      'has_archive' => true,

This code creates a new custom post type ‘Books’. register_post_type() function is used for this purpose. ‘init’ action hook is used to trigger this function.

Display all ‘Books’ posts

// Create a new WP_Query instance
$query = new WP_Query(array('post_type' => 'books'));

// Loop through the query results
if ($query->have_posts()) {
  while ($query->have_posts()) {

    // Display the post title
    echo '<h2>' . get_the_title() . '</h2>';

This code fetches and displays all posts of the ‘Books’ custom post type. We create a new WP_Query instance and pass the post type as ‘books’ to fetch all ‘Books’ posts.

Add a new ‘Books’ post

// Add new 'Books' post
$book_id = wp_insert_post(array(
  'post_title'    => 'My New Book',
  'post_content'  => 'This is my new book.',
  'post_status'   => 'publish',
  'post_type'     => 'books'

This code adds a new post of the ‘Books’ custom post type. We use the wp_insert_post() function to add a new post.

Update a ‘Books’ post

// Update 'Books' post
$updated = wp_update_post(array(
  'ID'            => $book_id,
  'post_content'  => 'This is my updated book.'

This code updates a post of the ‘Books’ custom post type. We use the wp_update_post() function to update a post.

Delete a ‘Books’ post

// Delete 'Books' post
$deleted = wp_delete_post($book_id);

This code deletes a post of the ‘Books’ custom post type. We use the wp_delete_post() function to delete a post.