1

I wrote a function to insert content in the page and every date update automatic .

I used wp_update_post( $my_post ); to update page .

I want first insert content in top specific page and then update, but it isn't working.

it's only updating or inserting content..

function wp_emallmobnok(){
        $postdater =  parsidate('j / F / Y',$datetime='now',$lang='pre');
        $postdate = date('Y-m-d H:i:s');
        $ta = parsidate('j / F / Y',$datetime='now',$lang='pre');
        $postdate_gmt = date('Y-m-d H:i:s');
        $titles="price daily";
        $posts = array(

            'post_content'   => $oiobz1,
            'post_name'      => $titles,/// The page url name
            'ID'             => 225, /// The page id witch we want to update that
            'post_title'     => $titles,
            'post_type'      => 'page',
            'post_status'    => 'publish',
            'post_author'    => '6',
            'ping_status'    => 'open',

            'post_date'  => $postdate_gmt,
            'post_category' => array(188),
            'tags_input'     => " price",
        );


        $post_id = wp_insert_post($posts);//// instert post
        add_post_meta( $post_id, ' wp_insert_post', 0, true );

        $post_up = wp_update_post($posts);///update post
        add_post_meta( $post_id, ' wp_update_post', 0, true );


}

1 Answer 1

1

As Codex says about wp_update_post:

To work as expected, it is necessary to pass the ID of the post to be updated.

Filling out the ID field is not strictly necessary but without it there is little point to using the function.

So you must add newly created post ID to $posts to update it.

function wp_emallmobnok(){
    $postdater    =  parsidate('j / F / Y',$datetime='now',$lang='pre');
    $postdate     = date('Y-m-d H:i:s');
    $ta           = parsidate('j / F / Y',$datetime='now',$lang='pre');
    $postdate_gmt = date('Y-m-d H:i:s');
    $titles       ="price daily";

    // Post data
    $posts        = array(
        'post_content'  => $oiobz1,
        'post_name'     => $titles,
        'post_title'    => $titles,
        'post_type'     => 'post',
        'post_status'   => 'publish',
        'post_author'   => '6',
        'ping_status'   => 'open',
        'post_date'     => $postdate_gmt,
        'post_category' => array(188),
        'tags_input'    => " price",
    );


    $post_id = wp_insert_post($posts);//// instert post
    add_post_meta( $post_id, ' wp_insert_post', 0, true );

    // Update post if inserting was successful
    if( $post_id !== 0 && !is_wp_error( $post_id ) ){
      // Add post ID to post data
      $posts['ID'] = intval( $post_id ); // ID has to be integer

      $post_up = wp_update_post($posts);///update post
      add_post_meta( $post_id, ' wp_update_post', 0, true );
    }
    else if( is_wp_error( $post_id ) ){
      $error_string = $post_id->get_error_message();
      echo 'ERROR: '.$error_string;
    }
}
4
  • not working. sending dublicate post. and no update @Rene Korss Commented Apr 30, 2015 at 7:31
  • Check the value of $post_id after $post_id = wp_insert_post($posts);. Maybe it has some errors. I edited my post to check for errors. Commented Apr 30, 2015 at 7:41
  • only publishing duplicate post Commented May 2, 2015 at 6:34
  • The ID should be integer but in my code was strings. I changed to integer and add 'ID'with number, below 'post_name'.. Thanks very much Commented May 3, 2015 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.