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 badge2 Answers
Reset to default 1This 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
版权声明:本文标题:custom taxonomy - wp_insert_post via shell category not being inserted 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744649093a2617576.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论