Problem with Create Post using metaWeblog.newPost or wp_insert_post

I have succesfully create new post by using the api metaWeblog.newPost. I can see the new post created under the All Posts admin page. But the problem is it won’t show on the homepage. Ii will only show after I go to the edit page of the new post and click Update. What could be causing the problem here?

The above scenario also happen even i use the wp_insert_post function. The new post is successfully created but won’t show on homepage.

It will only show after i click the update button in the edit post page. (Nothing is really being updated the title, the content still the same. All i did was just click the update button).

This is the code i used:

    <?php

    $BLOGURL = "http://xxxx/wordpress";
    $USERNAME = "xxxx";
    $PASSWORD = "xxxx";

    function get_response($URL, $context) {
    if(!function_exists('curl_init')) {
    die ("Curl PHP package not installed\n");
    }

    /*Initializing CURL*/
    $curlHandle = curl_init();

    /*The URL to be downloaded is set*/
    curl_setopt($curlHandle, CURLOPT_URL, $URL);
    curl_setopt($curlHandle, CURLOPT_HEADER, false);
    curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
    curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $context);

    /*Now execute the CURL, download the URL specified*/
    $response = curl_exec($curlHandle);
    return $response;
    }

    /*Creating the metaWeblog.newPost request which takes on five parameters
    blogid,
    username,
    password*/

    /*The title of your post*/
    $title = "Sample Post Title";

    /*The contents of your post*/
    $description = "This is a sample post.";

    /*Forming the content of blog post*/
    $content['title'] = $title;
    $content['description'] = $description;
    $content['categories'] = array("mycategoryname");
    /*Whether the post has to be published*/
    $toPublish = true;
    $request = xmlrpc_encode_request("metaWeblog.newPost",
    array(1,$USERNAME, $PASSWORD, $content, $toPublish));

    /*Making the request to wordpress XMLRPC of your blog*/
    $xmlresponse = get_response($BLOGURL."/xmlrpc.php", $request);
    $response = xmlrpc_decode($xmlresponse);

   /*Printing the response on to the console*/
    print_r($response);
    echo "\n";
    ?>

1 Answer
1

Probably because your post is saved as draft. Try to set the post status to publish inside your $content array. E.g.

$content['post_status'] = 'publish';

Leave a Comment