Create Post Automatically

This will create a Post using PHP.

<pre class="wp-block-syntaxhighlighter-code"><?php
     Code SOURCE:
      <blockquote class="wp-embedded-content" data-secret="HobdKZDRPY"><a href="">wp_insert_post()</a></blockquote><iframe title="&#8220;wp_insert_post()&#8221; &#8212; WordPress Developer Resources" class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);" src="" data-secret="HobdKZDRPY" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>	
//set timezone

//require native WordPress function

// Post variables
	$userID = get_current_user_id();
	$leadTitle='Operation Test Post'
	$leadContent='Hello there.  this is the post!';
     //ID# automatically assigned to categories.  For multiple
     // categories, separate with a comma
//Time variables - Sets time for now so post can be published without delay
	$timeStamp = date_create();
	$postdate = date("Y-m-d H:i:s", $timeStamp);	

//wordpress Array and Variables for posting
	$new_post = array(
//THIS submits the array data to creat the new post
	$post_id = wp_insert_post($new_post);

//Error Checking
     if ($post_id){
          echo "SUCCESS!";
     } else {
          echo 'OOPS!  something failed, please try again!';

Leave a Reply

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