如何将文章状态从draft改为publish?及WP To Twitter插件调用问题
Hey there! Let’s break down your two WordPress questions with practical, actionable solutions:
You’ve got a few solid options depending on your use case:
- Manual Update (Quickest for Single Posts)
Head to your WordPress dashboard, locate the draft post you want to publish. Either click the "Publish" button in the top-right corner of the post editor, or use the "Quick Edit" option on the posts list page—just switch the status dropdown to "Publish" and save. - Code Implementation (For Automation/Bulk Updates)
Use WordPress’s built-inwp_update_post()function to programmatically update the status. Here’s a straightforward example:
Pro tip: If adding this to your theme’s// Replace 123 with your actual post ID $post_id = 123; $update_args = array( 'ID' => $post_id, 'post_status' => 'publish' ); wp_update_post($update_args);functions.php, wrap it in a conditional hook (like a custom admin action) to avoid accidental runs. - WP CLI Command (Server-Side Efficiency)
If you have WP CLI set up, run this command to update a single post:
For bulk-updating all draft posts, use this one-liner:wp post update 123 --post_status=publishwp post update $(wp post list --post_status=draft --format=ids) --post_status=publish
Your hunch is exactly right—this issue almost always happens because custom fields and taxonomy terms are saved after the plugin triggers its tweet (which typically runs on the publish_post hook, default priority 10). Here’s how to fix it:
Adjust Hook Priorities
Ensure your custom data is saved before the plugin runs, or delay the plugin’s tweet trigger:
- If you’re using
save_postto save custom fields, set its priority lower than 10 (so it executes earlier):add_action('save_post', 'your_custom_field_saving_function', 5, 3); - Or, unhook the plugin’s default action and reattach it with a higher priority (so it runs later):
remove_action('publish_post', 'wpt_tweet'); add_action('publish_post', 'wpt_tweet', 20);
Manually Trigger the Tweet After Saving Data
Take full control by saving your custom data first, then calling the plugin’s tweet function directly:
$post_id = 123; // 1. Set post to publish status wp_update_post(array('ID' => $post_id, 'post_status' => 'publish')); // 2. Save your custom field and taxonomy terms update_post_meta($post_id, 'your_custom_field_key', 'your_field_value'); wp_set_post_terms($post_id, 'your_term_name', 'your_taxonomy_slug'); // 3. Fetch the data you need for the tweet $custom_field_val = get_post_meta($post_id, 'your_custom_field_key', true); $terms = get_the_terms($post_id, 'your_taxonomy_slug'); $term_name = $terms ? $terms[0]->name : ''; // 4. Customize the tweet content using the plugin's filter add_filter('wpt_custom_tweet', function($tweet_content, $post) use ($custom_field_val, $term_name) { return $tweet_content . " | " . $custom_field_val . " | " . $term_name; }, 10, 2); // 5. Manually trigger the tweet if the plugin function exists if (function_exists('wpt_tweet')) { wpt_tweet($post_id); }
Use transition_post_status Instead of publish_post
This hook fires after the post status has fully transitioned, ensuring all post data (including custom fields/terms) is saved:
remove_action('publish_post', 'wpt_tweet'); add_action('transition_post_status', function($new_status, $old_status, $post) { // Only trigger when moving to publish from a non-publish status if ($new_status === 'publish' && $old_status !== 'publish') { wpt_tweet($post->ID); } }, 10, 3);
内容的提问来源于stack exchange,提问作者Robert Andrews

