How to solve undefined wp_insert_post error?

Viewed 16

I am trying to post on WordPress but the wp_insert_post gives an error like this.

Undefined function 'wp_insert_post'.

I tried to solve this error by loading the wp-load.php file but I could not get any resolved. I searched for other ways to solve but there was only the wp-load.php way. So is there anyway else I can solve with? Here is my code:

<?php

date_default_timezone_set('Europe/Madrid');
$script_tz = date_default_timezone_get();
$currentDay = date('D');   //$d = date(ā€˜D’);
$formatter = new IntlDateFormatter('es_ES', IntlDateFormatter::FULL, IntlDateFormatter::NONE, 'Europe/Madrid', IntlDateFormatter::GREGORIAN);
$data1 = $formatter->format(time());

//to use built in wordpress function
require_once ('./wp-load.php');

//post variables
$postType = 'post';
$userID = 1;
$categoryID = '2';
$postStatus = 'publish';

$leadTitle = "Test post date:".$data1."";
$leadContent = '<h1>First Post</h1><p>This is my first post on wordpress.</p>';
$leadContent .= '<!--more--> <p>I am learning to post on wordpress.</p>';


$new_post = array(
    'post_title' => $loadTitle,
    'post_content' => $leadContent,
    'post_status' => $postStatus,
    'post_author' => $userID,
    'post_type' => $postType,
    'post_category' => array($categoryID)
);

//Wordpress post function
$post_id = wp_insert_post($new_post);

$finaltext = '';

if($post_id){
    $finaltext = 'Yes, I made a new post.<br>';
}else{
    $finaltext .= 'Something went wrong.<br>';
}

echo $finaltext;
?>
0 Answers
Related