Pre-populate WordPress posts

This post is about how to pre-fill some fields when opening the Add new posts form (/wp-admin/post-new.php). It’s straight forward for some fields like title and post type but more difficult for other fields. Actually there is no central resource for this and meanwhile I’m still thinking where to include it on WordPress codex, I started to document it here.

Fields to pre-fill with GET variables

Post type, title, content and excerpt can directly be pre-populated passing them as GET variables when calling the Add new post form:

/wp-admin/post-new.php?post_title=My Title&post_type=post&excerpt=Short excerpt

That’s because an empty post is created by get_default_post_to_edit() as this post explains in more details: This function reads the post_title, content and excerpt values in the $_REQUEST array, and also filters them through default_title, default_content and default_excerpt.

Better than passing the whole content as GET variable is is to apply the default_content filter. My use case was to copy a yet existing post so I passed a reference to the parent post ID as GET variable named post_parent:

add_filter( 'default_content', 'content_by_request', 10, 2 );

function content_by_request( $content, $post ) {
	//set content
	if (!empty( $_GET['post_parent'] )) {
		$parent_id = $_GET['post_parent'];
		$content = get_post($parent_id)->post_content;
    return $content;

Fields to pre-fill with actions and filters

All the other fields need to be read from the $_REQUEST array, a typical example would be passing them as GET variables. The following code shows how to pre-fill category, tags, custom fields and featured image:

add_action( 'load-post-new.php', 'myplugin_post_new' );
function myplugin_post_new(){
	add_action( 'wp_insert_post', 'myplugin_wp_insert_post_default' );
function myplugin_wp_insert_post_default( $post_id ){
	//set category
	$cat_id = $_REQUEST['tax-input']['event-category'];
	wp_set_post_terms( $post_id, $cat_id, 'event-category' );

	//set tags
	wp_set_post_tags( $post_id, $_REQUEST['tags']);

	//set custom field
	add_post_meta( $post_id, 'myplugin_meta_key', $_REQUEST['meta_value'] );

	//set thumbnail and use the one from parent post
	if (!empty( $_GET['post_parent'] )) {
		$parent_id = $_GET['post_parent'];
		$attached_image = get_children( "post_parent=".$parent_id."&post_type=attachment&post_mime_type=image&numberposts=1" );
		if ($attached_image) {
			foreach ($attached_image as $attachment_id => $attachment) {
				set_post_thumbnail($post_id, $attachment_id);

Pre-fill author using wp_insert_post_data filter

One way to pre-populate author field is to pass the author ID as GET variable to the Add new post form:


Then you read the GET variable and apply wp_insert_post_data filter:

function myplugin_wp_set_post_author( $data , $postarr ){
	if (!empty( $_GET['author'] )) {
		$author = $_GET['author'];
	else {
		$author = 1;
	$data['post_author'] = $author;
	return $data;
add_filter('wp_insert_post_data' , 'myplugin_wp_set_post_author' , '99', 2);

Plugin fields to pre-fill

Normally the best method is to pass the parameters as GET variable and pre-populate the fields with a plugin filter.

One example I needed recently is to prefill Event Organiser start and end date and time. This plugin offers the filter eventorganiser_get_event_schedule which (thanks to this post) can be used in the folloging way:

add_filter('eventorganiser_get_event_schedule', 'myplugin_prefill_date', 10, 2);

function myplugin_prefill_date($event_details, $post_id) {

	if ( !isset($_GET) || count($_GET) === 1 ) return $event_details;

	if ( isset($_GET['start']) ) {
		$hm = explode("-", $_GET['start'] );
		date_date_set($event_details['start'], $hm[2], $hm[1], $hm[0]);

	if ( isset($_GET['end']) ) {
		$hm = explode("-", $_GET['end'] );
		date_date_set($event_details['end'], $hm[2], $hm[1], $hm[0]);

	//set standart time
	date_time_set($event_details['start'], 0, 0);
	date_time_set($event_details['end'], 0, 0);

	return $event_details;

Tags: ,

Leave a Reply