我的脚本使用代码设置帖子内容。我也在运行WP To Twitter 插件,自动推特这些帖子。
问题是,虽然插件使用默认的WordPress帖子字段正确地构建推文,但它没有使用可用的自定义帖子字段,也没有使用帖子所附加的分类法的术语名称。
插件作者探讨的理论是,这些自定义字段和术语正在被保存after 帖子本身已经发布,这意味着它们在推特上是看不见的。
这看起来是真的。我的代码。。。
使用wp_insert_post
将包含默认字段的帖子另存为post_status
publish
然后使用添加自定义字段update_field
然后设置分类术语作者建议对我的代码进行调整:
将默认的post字段另存为draft
然后使用添加自定义字段update_field
且仅,然后将post状态更改为publish
在下面的脚本摘录中,我试图进行更改-更改为
draft
并添加
wp_update_post
在两者中
if
条款。
在某种程度上,这是可行的-我可以看到脚本现在正在将新帖子设置为draft
.
What is not working, however, is the transfer of these posts to publish
, they remain in draft
. This is what I need to solve - to ensure that they can be set as publish
.
出什么问题了
wp_update_post
以及
my_post
大堆数组中值的顺序是否重要?比如,应该
ID
先来吧?是否未设置问题
post_date
或
post_date_gmt
?
使用是否更好/不同wp_publish_post
而不是wp_update_post
? 我读到有一些差异。
//if quote is not found in the quotes custom post type
//it will create new quotes and save it in to the db
if($quote_id == 0){
echo "Quote is not found. Creating new quote<br />";
$customPostParams = array ( \'post_author\' => $user_id,
\'post_type\' => \'quote\',
\'post_title\' => $story[\'title\'],
\'post_content\' => $body,
// WP To Twitter plugin fires on publish and can only see published post data.
// Quote story_* fields, Source terms, Company terms and Product $firm_terms are set
// after saving.
// So, let\'s save as draft, set those items and only publish when all items are available.
\'post_status\' => \'draft\', // was: \'publish\'
\'post_date\' => $story[\'published_at\']->format(\'Y-m-d H:i:s\'),
\'post_date_gmt\' => $story[\'published_at\']->format(\'Y-m-d H:i:s\'),
\'comment_status\' => \'closed\',
\'ping_status\' => \'closed\' );
$post_id = wp_insert_post($customPostParams);
update_field(\'story_id\', $story[\'id\'], $post_id);
update_field(\'story_permalink\', $story[\'links\'][\'permalink\'], $post_id);
update_field(\'story_language\', $story[\'language\'], $post_id);
update_source_and_connect_to_quote($post_id, $story);
update_companies_and_connect_to_quote($post_id, $story, $firmName, $firm_alternate_name);
update_product_and_connect_to_quote($post_id, $story);
// Change from draft to published
$my_post = array(
\'post_type\' => \'quote\',
\'ID\' => $quote_id,
\'post_status\' => \'publish\',
);
// Update the post into the database
wp_update_post( $my_post );
} else {
//if quotes already found then it will update that quote
echo "Quote is found. Updating existing quote<br />";
echo "Title: {$story[\'title\']} <br />";
$customPostParams = array ( \'ID\' => $quote_id,
\'post_author\' => $user_id,
\'post_type\' => \'quote\',
\'post_title\' => $story[\'title\'],
\'post_content\' => $body,
\'post_status\' => \'draft\',
\'post_date\' => $story[\'published_at\']->format(\'Y-m-d H:i:s\'),
\'post_date_gmt\' => $story[\'published_at\']->format(\'Y-m-d H:i:s\'),
\'comment_status\' => \'closed\',
\'ping_status\' => \'closed\' );
wp_update_post( $customPostParams );
update_field(\'story_id\', $story[\'id\'], $quote_id);
update_field(\'story_permalink\', $story[\'links\'][\'permalink\'], $quote_id);
update_field(\'story_language\', $story[\'language\'], $quote_id);
update_source_and_connect_to_quote($quote_id, $story);
update_companies_and_connect_to_quote($quote_id, $story, $firmName, $firm_alternate_name);
update_product_and_connect_to_quote($quote_id, $story);
// Change from draft to published
$my_post = array(
\'post_type\' => \'quote\',
\'ID\' => $quote_id,
\'post_status\' => \'publish\',
);
// Update the post into the database
wp_update_post( $my_post );
}