Wp_insert_post Example - Kvcodes

Congrats, You are Subscribed to Receive Updates.

Wp_insert_post Example


wp_insert_post example and wp_insert_post in WordPress and wp_insert_post custom post type. wp_insert_post featured image example. Many articles and many examples already there in many blogs and forums. Here I am going to explain nothing but how to use it effectively the wp_insert_post to add new post with your program.


The below function helps to insert or update post with your post through programmatically. Already the syntax available in codec page. I just wrote in simply the syntax without much explanations.

	array(  'ID'   => (if its update, you have to provide existing post, you can use the ID to update it),  // optional
		'post_author'   => (author ID),   
		'post_content' => $_POST['content'],  // content of the article,
		'post_title' => $_POST['title'],  // title of the article
		'post_status' => 'publish',   // i just published the post directly, but you can change the status as of your need
		'post_type' => 'post',   // if any custom post type, you can use it here 
		'post_category' => array(25, 35),
		'tax_input' => array('WordPress', 'post')

The above one is simple  and I don’t want to give you all and make you feel uncomfortable with it.  Widely these arguments are used, if you want to know more and depth in parameter types, take a  look at its codec page.


Wp_insert_post Hook

There is one more thing, there is an action hook wp_insert_post, which helps to trigger certain functions and perform some operation while publishing or updating a post. here is the sample

function kvcodes_insert_post_hook($ID, $post, $update){
 // Here your code.
add_action('wp_insert_post', 'kvcodes_insert_post_hook');

If you wish to add a featured image to your post, just do the following thing.

Wp_insert_post Featured Image

There are several things together in a post. Sometimes you need to add a thumbnail image to the post. If you are already uploaded an image to media library and you have the attachment Id, than its so simple to do it. just do the following thing.

set_post_thumbnail($Post_ID, $attach_ID);

That’s it. If you need to upload the image and do this performance, you have to read my previous article.  Create Front-end Multiple File Upload – WordPress.


About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level

One comment

  1. commenter

    Great, it’s working for me.

    Thank you

    Best Regards

Comment Below

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


Current ye@r *



Subscribe E-mail Updates

Congrats, You are Subscribed to Receive Updates.


Category Posts