Wp_insert_post Example - Kvcodes
X

Congrats, You are Subscribed to Receive Updates.

Wp_insert_post Example


Introduction

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.

Syntax

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.

wp_insert_post( 
	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-Example

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.

commenter

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, OpenCart Theme Development, PHP, HTML, CSS, jQuery, Bootstrap development and content SEO.

Comment Below

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

*

Current ye@r *

Menu

Sidebar

Subscribe E-mail Updates

Congrats, You are Subscribed to Receive Updates.

Advertisement

Category Posts