My script uses code to set post content. I am also running the WP To Twitter plugin, to auto-tweet those posts.
The problem is, whilst the plugin is correctly formulating its tweets using the default WordPress post fields, it is not using available custom post fields, nor the term name of a taxonomy to which the post is attached.
The theory, explored with the plugin author, is this - those custom fields and terms are getting saved after the post itself is published, meaning they are invisible at the point of tweeting.
It looks true. My code...
- uses
wp_insert_postto save a post containing default fields aspost_statuspublish - then adds the custom fields using
update_field - and then sets the taxonomy terms.
Author advises a tweak to my code:
- save the default post fields instead as
draft - then add the custom fields using
update_field - and only then change the post status to
publish
In the excerpt from my script below, I have attempted to make the change - changing to draft and adding a wp_update_post in both if clauses.
This works, to a point - I can see the script is now setting new posts as 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.
What is going wrong with wp_update_post and the my_post array? Does the order of the values in the array matter? Like, should ID come first? Is the problem not setting post_date or post_date_gmt?
And would it be better/different to use wp_publish_post instead of wp_update_post? I read there are some discrepancies.
//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 ); }
update_fieldis not a WordPress API? Also are you aware that if you don't pass an ID towp_update_postit will create the post? It's just a wrapper aroundwp_insert_post, you should be able to reduce your codes size in half triviallyupdate_field- it is Advanced Custom Fields' function for updating ACF fields. But it works okay and saves them fine, the problem ispost_statusthereafter. 2. But I am passing an ID towp_update_post, I think. 3. Don't understand the code reduction point.quote_idin$my_postshould bepost_id. Going to try that.