Using WordPress ‘get_default_post_to_edit()’ PHP function

The get_default_post_to_edit() WordPress PHP function returns default post information to use when populating the “Write Post” form.

Usage

get_default_post_to_edit($post_type = 'post', $create_in_db = false);

Parameters

  • $post_type (string) Optional – A post type string. Default is ‘post’.
  • $create_in_db (bool) Optional – Whether to insert the post into the database. Default is false.

More information

See WordPress Developer Resources: get_default_post_to_edit()

Examples

Get default post

This example shows how to get the default post information.

// Get default post information
$default_post = get_default_post_to_edit();

// Display default post information
echo "Default Post Title: " . $default_post->post_title . "<br>";
echo "Default Post Content: " . $default_post->post_content;

Get default page

This example demonstrates how to get the default page information.

// Get default page information
$default_page = get_default_post_to_edit('page');

// Display default page information
echo "Default Page Title: " . $default_page->post_title . "<br>";
echo "Default Page Content: " . $default_page->post_content;

Create default post in database

This example shows how to create a default post in the database.

// Create a default post in the database
$new_post = get_default_post_to_edit('post', true);

// Display new post ID
echo "New Post ID: " . $new_post->ID;

Create default custom post type

This example demonstrates how to create a default custom post type.

// Register custom post type
function register_custom_post_type() {
    register_post_type('custom_type', array(
        'public' => true,
        'label' => 'Custom Type'
    ));
}
add_action('init', 'register_custom_post_type');

// Get default custom post type information
$default_custom_type = get_default_post_to_edit('custom_type');

// Display default custom post type information
echo "Default Custom Type Title: " . $default_custom_type->post_title . "<br>";
echo "Default Custom Type Content: " . $default_custom_type->post_content;

Modify default post information

This example shows how to modify the default post information.

// Get default post information
$default_post = get_default_post_to_edit();

// Modify default post information
$default_post->post_title = "Modified Title";
$default_post->post_content = "Modified Content";

// Display modified post information
echo "Modified Post Title: " . $default_post->post_title . "<br>";
echo "Modified Post Content: " . $default_post->post_content;