lundi 2 mars 2015

Insert post programmatically and decide to tweet or not to tweet


I am inserting a new post using wp_insert_post() and want to be able to decide if this post gets tweeted or not using the WP to Twitter plugin.


First, I create the post, like so:



$newpost = array(
'post_title' => 'Hello world',
'post_content' => 'Is it a good day to tweet or not?',
'post_status' => 'draft'
);
$newpostid = wp_insert_post($newpost);


Second, I update the postmeta table by setting:



if($tweet_this_post == true) {
update_post_meta($newpostid, '_jd_tweet_this', 'yes');
}
else {
update_post_meta($newpostid, '_jd_tweet_this', 'no');
}


Thus far, everything works as expected. The post gets created and the postmeta table gets set with the option _jd_tweet_this to be either yes or no. So far, so good.


However, since I set up the WP to Twitter to tweet all new posts by default, the postmeta table always already contains the _jd_tweet_this option set to yes by default. And I cannot override it by using the update_post_meta() function as described above.


When I set $tweet_this_post to false, I end up with two (2) entries for _jd_tweet_this in the postmeta table - the default yes has a lower meta_id value than the value inserted by my update_post_meta() function. Which indicates to me, that the default value (yes) was inserted BEFORE the programmatically added value (no), which suggests that the update_post_meta() function IS NOT UPDATING the existing value for _jd_tweet_this.


My question: Can anybody explain to me what I need to do to programmatically control if an inserted post in draft status gets tweeted when I manually publish it from the dashboard?





Aucun commentaire:

Enregistrer un commentaire