wordpress create post programmatically

Wordpress Tutorials, WrodPress Programmatically

wordpress create post programmatically

create post programmatically

We have already discussed how to create a post in WordPress but at WordPress, development time need WordPress to create post programmatically. In WordPress development, it’s most important things we always know about the post because WordPress is all about the post.

So before start programming to insert post in WordPress first we need to learn about insert post function that is built in function provided by WordPress and that function name is wp_insert_post() that functions need argument array that contains post information.

wp_insert_post(array $post_arr, bool $wp_error = false) 

$post_arr

That is the first parameter that uses in that function and that is a required parameter that contains all the parameter that contains details about the post. Below is the details description of that array.

  • ID: Id is the post id that we can mention the post id if want otherwise the default is 0.
  • post_author: That is the id that mentions who added the post we can mention user id otherwise by default that post id the currently logged in user id.
  • post_date: As name such as that is the date of the post, the default is the current time.
  • post_title: That contains the post title that is unique for every post so for that, you need to check before inset the new post.
  • post_name: That is the slug of the post and you can say URL of the post that is also need unique that WordPress will create automatically if you will not provide that.
  • post_content: That contains the actual content and body of the post.
  • post_status: Set the post status on the insert time whether the post will be live mean publish or in the draft.
  • post_type: Mention that post type at the insert time because WordPress allowing to create custom post type so we can also insert post in our custom post type also default post type is a post.

$wp_error

That is whether the return WordPress error on the time of insert post and that parameter is optional.

So, that is all the basics parameters for insert post in WordPress so below is the also provide code that will show to programmatically insert post in WordPress.

function wordpress_create_post_programmatically() {

  $author_id = 1;
  $slug = 'helpyouforever-post';
  $title = 'My Helpyouforever Post';
  $content = 'Hello friends i am Helpyouforever and that is my first post that will display you how insert post programatically. And that is test content';
  // If the post doesn't already exist, then create it only create new post
  if( null == get_page_by_title( $title, 'OBJECT', 'post') ) {//Check that post already avilable or not in wordpress

    $post_arr = array(
    'post_title' => $title,
    'post_name' => $slug,
    'post_content' => $content,
    'post_author' => $author_id,
    'post_status' => 'publish',
    'post_type' => 'post'
   );
   $post_id = wp_insert_post($post_arr);
  }

} //end wordpress_create_post_programmatically
add_filter( 'after_setup_theme', 'wordpress_create_post_programmatically' );

 

Using that code you post will be inserted successfully you can check that from WordPress admin area. In that above example, I have used an only minimum parameter of the post you can add more parameter as per your requirement and make sure above listed parameter is also is minimum you can refer WordPress org to get knowledge about all the parameters in details.

Below is a screenshot of the created post in my WordPress setup.

wordpress create post programmatically 1

7 thoughts on “wordpress create post programmatically

  1. I together with my buddies have been looking through the great helpful hints from the website and so instantly developed an awful feeling I had not thanked the site owner for those secrets. All of the young boys came certainly passionate to learn them and already have certainly been loving these things. Appreciation for simply being so considerate and for making a decision on these kinds of magnificent useful guides millions of individuals are really desperate to understand about. My personal sincere regret for not saying thanks to you earlier.

  2. Excellent post. I was checking constantly this blog and I am impressed! Very useful info specifically the last part 🙂 I care for such information much. I was seeking this particular information for a very long time. Thank you and good luck.

  3. Wonderful blog! Do you have any suggestions for aspiring writers? I’m hoping to start my own website soon but I’m a little lost on everything. Would you advise starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m totally overwhelmed .. Any ideas? Thanks!

Leave a Reply