I'm using this code:

$post = array(
        'post_content' => $desc,
        'post_title' => $name_new,
        'post_status' => 'publish',
        'post_type' => 'portfolio_page',
        'tax_input' => array('portfolio_category' => array($cat, 18, 19))

    if (!$post_id = wp_insert_post($post)) {

which is working perfectly when executed via web browser. Unfortunately I need to execute it via shell, and what is happening is the post is inserted but the tax_input is ignored.

Any suggestion?

thanks a lot


This is typical issue. While wp_insert_post() won't check for user capabilities to insert post, the tax_input part actually will check if user has permission to manipulate those specific taxonomies.

If you don't have user logged in state replicated in your shell context then it will fail accordingly.

The solution is to insert post first, then assign terms via separate function calls which won't check capabilities (yeah, it's not consistent).

|improve this answer|||||

As has already been pointed out, to pass the tax_input argument to wp_insert_post() you need to have the user logged in. Which, in your case, trying to execute this from the shell, is of no use.

Alternatively, you could use XML-RPC to create the post.

// Function to post to WordPress using XML-RPC
function wp_post_xmlrpc( $username, $password, $rpc_url, $title, $body, $post_type, $categories ) {

    $title = htmlentities( $title, ENT_NOQUOTES, 'UTF-8' );

    $content = array(
        'title'             => $title,
        'description'       => $body,
        'mt_allow_comments' => 0,  // 1 to allow comments
        'mt_allow_pings'    => 0,  // 1 to allow trackbacks
        'post_type'         => $post_type,
        'categories'        => array( $categories ), 

    $params = array( 0, $username, $password, $content, true );

    $request = xmlrpc_encode_request( 'metaWeblog.newPost', $params );

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    curl_setopt($ch, CURLOPT_URL, $rpc_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $results = curl_exec($ch);


    return $results;    // Returns the ID of the post


$example_post = wp_post_xmlrpc( 'username', 'password', 'http://www.mysite.com/xmlrpc.php', 'Example XML-RPC Post', 'This is an example post using XML-RPC', 'post', array( 'category_one', 'category_two', 'category_three' ) );

echo $example_post; // Echoes out the ID of the post

The code and the example should be pretty much self-documenting.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.