Using WordPress ‘get_post_to_edit()’ PHP function

The get_post_to_edit() WordPress PHP function retrieves an existing post and formats it for editing.

Usage

To use the get_post_to_edit() function, simply provide the post ID as an argument:

$edited_post = get_post_to_edit( $post_id );

Parameters

  • $id (int) (Required) – The ID of the post you want to retrieve and format for editing.

More information

See WordPress Developer Resources: get_post_to_edit

Examples

Get a post to edit and display the title

Retrieve a post by its ID and display its title:

$post_id = 42;
$edited_post = get_post_to_edit( $post_id );
echo 'Post Title: ' . $edited_post->post_title;

Get a post to edit and display the content

Retrieve a post by its ID and display its content:

$post_id = 42;
$edited_post = get_post_to_edit( $post_id );
echo 'Post Content: ' . $edited_post->post_content;

Get a post to edit and display the excerpt

Retrieve a post by its ID and display its excerpt:

$post_id = 42;
$edited_post = get_post_to_edit( $post_id );
echo 'Post Excerpt: ' . $edited_post->post_excerpt;

Get a post to edit and display the status

Retrieve a post by its ID and display its status:

$post_id = 42;
$edited_post = get_post_to_edit( $post_id );
echo 'Post Status: ' . $edited_post->post_status;

Get a post to edit and display the author

Retrieve a post by its ID and display its author ID:

$post_id = 42;
$edited_post = get_post_to_edit( $post_id );
echo 'Post Author ID: ' . $edited_post->post_author;