This website uses cookies to allow us to see how the site is used. If you continue to use this site, we assume that you are okay with this. If you want to use the sites without cookies, please see our privacy policy.

Programmatically Create a WordPress Page, Post, or Custom Post Type

Programmatically creates a WordPress post based on the incoming parameters.
/**
 * Programmatically creates a WordPress post based on the incoming parameters.
 *
 * Note: This function may need some additional work if you're dealing with non-English languages.
 *
 * @param	string	$title		The title of the page as presented to the users
 * @param	string	$slug		The slug used to access the page via the URL
 * @param	string	$post_type	The type of post to create. Can be either 'page' or 'post'
 * @param	string	$template	The template to apply to the page.
 * @return	int			The ID of the page that was created. -1 if the page already exists.
 */
function wp_snippets_create_post( $title, $slug, $post_type, $template = null ) {

	// Initialize the page ID so we know if it's been created
	$page_id = -1;

	// If a post doesn't already exist with the specified title, create it
	if( null == wp_snippets_get_permalink_by_title( $slug, $post_type ) ) {

		$page_id = wp_insert_post(
			array(
				'comment_status'	=>	'open',
				'ping_status'		=>	'open',
				'post_author'		=>	1,			// Administrator is creating the page
				'post_title'		=>	$title,
				'post_name'		=>	strtolower( $slug ),
				'post_status'		=>	'publish',
				'post_type'		=>	strtolower( $post_type )
			)
		);

		// If a template is specified in the function arguments, let's apply it
		if( null != $template ) {
			update_post_meta( get_the_ID(), '_wp_page_template', $template );
		} // end if

	} // end if

	return $page_id;

} // end wp_snippets_create_post

/**
 * Determine if a post or page already exists with the specified title.
 *
 * Note: This function may need some additional work if you're dealing with non-English languages.
 *
 * @param	string	$title		The title used to check if the post exists.
 * @param	string	$post_type	The post type that we're checking (could be post, page, or a custom post type)
 * @return	string				The permalink to the post. Null if the post or page doesn't exist.
 */
function wp_snippets_get_permalink_by_title( $title, $post_type ) {

    // Initialize the permalink value
    $permalink = null;

    // Try to get the page by the incoming title
    $post = get_page_by_title( strtolower( $title ), OBJECT, $post_type );

    // If the post exists, then let's get its permalink
    if( null != $post ) {
        $permalink = get_permalink( $post->ID );
    } // end if

    return $permalink;

} // end wp_snippets_get_permalink_by_title

About

I'm a Senior Backend Engineer at WebDevStudios and I can often be found of software engineering within the context of blogging on my own site.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.