Programmatically create WordPress Posts

Create post by code is very easy in wordpress. Defining and setting post variables
set the post variables to be passed as an array object to the wp_insert_post() function.
There are quite a few post variable parameters that wp_insert_post() accepts, but for the sake of this tutorial, use the following:
    postType – set this variable to ‘post’
    userID – Post Author Id.
    categoryID – set a single Post category id
    leadTitle – set this to the desired Post title
    leadContent – set this to be the HTML string for your post’s content
Once the new_post variable array has been created, it can now be passed to the wp_insert_post() function as an argument.
The final step is creating a post_id variable and set it equal to wp_insert_post(). Upon successful entry of a page or post in WordPress, the wp_insert_post() function returns an id.
Last but not least, we use a simple if else statement to determine whether wp_insert_post() function return an id. If successful, then post insertion executed. If not, then post insertion execution failed.
We then echo the finaltext variable to the web browser display.  That’s it.
<?PHP
// require wp-load.php to use built-in WordPress functions In your custom file

 

/*******************************************************
** POST VARIABLES
*******************************************************/

 

/*******************************************************
** TIME VARIABLES / CALCULATIONS
*******************************************************/
// VARIABLES

 

/*******************************************************
** WordPress Array and Variables for posting New Post
*******************************************************/

 

/*******************************************************
** WordPress Post Function
*******************************************************/

 

/*******************************************************
** SIMPLE POST ERROR CHECKING
*******************************************************/

 

I, Dayanand Saini,the founder and chief editor of www.webtechsource.com,have been working as a software engineer from last 4+ years. It's my hobby to learn new things and implement on live environment.

Leave a Reply

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

64 − = 55

%d bloggers like this: