如何将帖子状态从“征求意见稿”改为“发布”?

时间:2018-02-07 作者:Robert Andrews

我的脚本使用代码设置帖子内容。我也在运行WP To Twitter 插件,自动推特这些帖子。

问题是,虽然插件使用默认的WordPress帖子字段正确地构建推文,但它没有使用可用的自定义帖子字段,也没有使用帖子所附加的分类法的术语名称。

插件作者探讨的理论是,这些自定义字段和术语正在被保存after 帖子本身已经发布,这意味着它们在推特上是看不见的。

这看起来是真的。我的代码。。。

使用wp_insert_post 将包含默认字段的帖子另存为post_status publishupdate_field

将默认的post字段另存为draftupdate_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_datepost_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 );
    
    
                }
    

    1 个回复
    SO网友:Robert Andrews

    下面由两部分组成的答案-

    如上所述:广泛的解决方案是先将帖子保存为草稿,然后使用自定义字段和术语更新帖子,然后再将其设置为发布。

    正如我所说,上述代码修改成功地将职位设置为draft 但不是将其转换为publish, 他们被卡住了。

    确实,我在第一次使用错误的变量if 条款Solution: 在阵列馈送中wp_update_post(), 正在更改\'ID\' => $quote_id,\'ID\' => $post_id, 成功帮助职位过渡到publish 在最后。这只是我的一个错误,真的publish 被解雇了。Solution: 改变状态转换的方法wp_update_postwp_publish_post 让它看到一切

                    // Change from draft to published
                    $my_post = array(
                      \'ID\'           => $post_id,
                      \'post_type\' => \'quote\',
                      \'post_status\'   => \'publish\',
                    );
                    // Update the post into the database
                    wp_update_post( $my_post );
    
    到。。。

                    wp_publish_post( $post_id );
    
    完成后,现在looks 好像一切正常-插件正在推特出我需要的所有帖子字段(标准和自定义)。

    阅读一些关于wp_update_postwp_publish_post, 因为我读过some information on here 关于差异,建议使用前者。

    结束

    相关推荐