admin管理员组

文章数量:1414908

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_post to save a post containing default fields as post_status publish
  • 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 );


            }

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_post to save a post containing default fields as post_status publish
  • 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 );


            }
Share Improve this question edited Feb 7, 2018 at 0:12 Robert Andrews asked Feb 7, 2018 at 0:04 Robert AndrewsRobert Andrews 9881 gold badge19 silver badges42 bronze badges 3
  • hmmm update_field is not a WordPress API? Also are you aware that if you don't pass an ID to wp_update_post it will create the post? It's just a wrapper around wp_insert_post, you should be able to reduce your codes size in half trivially – Tom J Nowell Commented Feb 7, 2018 at 0:19
  • @TomJNowell 1. Good point/question re update_field - it is Advanced Custom Fields' function for updating ACF fields. But it works okay and saves them fine, the problem is post_status thereafter. 2. But I am passing an ID to wp_update_post, I think. 3. Don't understand the code reduction point. – Robert Andrews Commented Feb 7, 2018 at 0:30
  • Got a feeling my quote_id in $my_post should be post_id. Going to try that. – Robert Andrews Commented Feb 7, 2018 at 0:44
Add a comment  | 

1 Answer 1

Reset to default 2

A two-part answer below -

As above: the broad solution was to save the posts first as draft, then update them with the custom fields and terms, and only then set them to publish.

As I said, the above code modification had succeeded in setting the posts to draft but not in transitioning them to publish, they were stuck.

  1. Indeed, I was using the wrong variable in the first if clause. Solution: In the array feeding wp_update_post(), changing 'ID' => $quote_id, to 'ID' => $post_id, succeeded in helping the posts transition to publish at the end. This was just a mistake on my part, really.
  2. However, then the WP To Twitter plugin was not seeing the post at all as publish was fired. Solution: It took changing the method of status transitioning from wp_update_post to wp_publish_post to make it see everything...

That is, from...

                // 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 );

to...

                wp_publish_post( $post_id );

After doing that, it now looks like it works out okay - the plugin is tweeting out all the post fields (standard and custom) that I need.

It would be useful to read some things about the differences between wp_update_post and wp_publish_post, because I had read some information on here about discrepancies, with a recommendation to use the former.

本文标签: How to change post status from quotdraftquot to quotpublishquot