admin管理员组

文章数量:1389903

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)) {
        die('Error');
    }

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

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)) {
        die('Error');
    }

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

Share Improve this question asked Nov 12, 2014 at 13:29 Andrea GiorginiAndrea Giorgini 211 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 1

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).

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);

    curl_close($ch);

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

}



$example_post = wp_post_xmlrpc( 'username', 'password', 'http://www.mysite/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.

本文标签: custom taxonomywpinsertpost via shell category not being inserted