0

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?

Updated: Here is the code, which is contained in my functions.php:

function wpcf7_to_post($cfdata) {
    $formtitle = $cfdata->title;
    if($formtitle == 'front-end-add-post') {
        $submission = WPCF7_Submission::get_instance();
        if($submission) {
            $formdata = $submission->get_posted_data();

            // insert new post with form data
            $newpost = array(
                'post_title' => trim($formdata['title']),
                'post_content' => trim($formdata['content']),
                'post_status' => 'draft',
                'post_type' => 'post'
            );
            $newpostid = wp_insert_post($newpost);

            // tweet this post?
            if(in_array('yes', $formdata['tweet'])) {
                update_post_meta($newpostid, '_jd_tweet_this', 'yes');
            }
            else {
                update_post_meta($newpostid, '_jd_tweet_this', 'no');
            }
        }
    }
}
add_action('wpcf7_before_send_mail', 'wpcf7_to_post', 1);

As you can see, I am using CF7 plugin for front page submission of new posts and I am reading the data from the form (this all works fine ;-))... Thanks!

  • 2
    update_post_meta updates an already existing meta field; it only creates new one row in the database if the field doesn't exist. Can you post the full code you are using? – cybmeta Mar 2 '15 at 8:50
  • Hi @cybmeta, thank you for your answer. Yes, update_post_meta will create a new entry if the meta does not exist in the postmeta table. The code above is basically the full code. Directly after creating the post (I read the input of a form submission), I update the post meta instantly thereafter... – Sebastian Mar 2 '15 at 9:00
  • 1
    You said that update_post_meta is inserting new row in the database instead of updating existing one, that is why we need to see the full code, otherwise we can not know if the problem is in your code or anywhere esle, for example in the plugin you are using (in this case you should contact with the developer as thrid party plugins are off-topic here). So, please, post the full code beacuse the information you have provided is not enough to answer the question, at least for me. – cybmeta Mar 2 '15 at 9:03
  • 1
    Please, add the code in the question and not in a external source. – cybmeta Mar 2 '15 at 10:47
  • 1
    In your code, I don't see anything that could be duplicating meta fields instead of updating them. You should contact with the developer of WP to Twitter plugin to ask him/her/them how the plugin creates the meta field. PD: This site is almost 100% about code, so there are not cluttering code fragments. – cybmeta Mar 2 '15 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.