WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Question about wp_insert_post (6 posts)

  1. giant slayer
    Member
    Posted 2 years ago #

    I have written a bit of code which updates the content on one of my pages at a regular given interval. I want to modify this so that it also creates and publishes a new post at the same time. I added the following lines of code insert a new post and update the post meta information.

    $new_post_id=wp_insert_post( $my_post );
    add_post_meta($new_post_id, 'aktt_notify_twitter', 'yes');
    add_post_meta($new_post_id, 'wpbook_fb_publish', 'yes');

    I populate $my_post with the post title, status, author, etc.

    Will this code trigger my twitter tools and wpbook plugins so that the post is also posted to fb and tweeted? Or do I have to do something with a hook and an action?

    thank you,
    christopher

  2. eryanv
    Member
    Posted 2 years ago #

    I'm going to guess that the twitter tools and wpbook plugin functionality occurs when the publish_post action occurs. So if those post metas aren't set at that time, it probably won't do anything with your post. Obviously you could test this out and verify that this is the case. Something you may want to consider is to set your post's status to "pending" or "draft", insert it, set your post metas, then use the wp_publish_post($new_post_id) function to publish the post.

  3. giant slayer
    Member
    Posted 2 years ago #

    So what if I did something like this?

    'post_date_gmt'=>strtotime('+5 minutes');
    'post_status' => 'future',

    I set the post status to future and set the post time 5 minutes later. Then when wp_insert_post is called, the post is created, the meta is added, and then the when the 5 minutes passes, the post is published.

  4. eryanv
    Member
    Posted 2 years ago #

    I would imagine that would work too, but then you'd have to wait 5 minutes (or however long you set) for your post to show up.

  5. giant slayer
    Member
    Posted 2 years ago #

    What about something like this? Then I could leave the post status to publish and have no time delay.

    function insert_required_meta_data($id) {
        add_post_meta($new_post_id, 'aktt_notify_twitter', 'yes');
        add_post_meta($new_post_id, 'wpbook_fb_publish', 'yes');
    }
    add_action('pre_post_update', 'insert_required_meta_data');
    wp_insert_post( $my_post );
  6. giant slayer
    Member
    Posted 2 years ago #

    This worked like a charm.

Topic Closed

This topic has been closed to new replies.

About this Topic