Title: Problem with wp_insert_post
Last modified: May 16, 2020

---

# Problem with wp_insert_post

 *  Resolved [infmgt](https://wordpress.org/support/users/infmgt/)
 * (@infmgt)
 * [6 years ago](https://wordpress.org/support/topic/problem-with-wp_insert_post-2/)
 * According to the Codex, when using wp_insert_post a page is created or updated
   depending upon the contents of $post_id. If this is zero, a new page is created.
   Otherwise, the post with the id which is set in $post_id will be updated.
 * I have a routine which creates a page using wp_insert_post. The routine works.
   However, if I try to update an already created new page, a second page is created
   rather than the old one being modified – even though the routine has a mechanism
   to avoid this:
 *     ```
       $list_name = $_REQUEST["list_name"];
   
       // Set (or get) the page id so as to create or update	
       $page_id = 0;	
       global $wpdb; // Code is within a function
   
       $sql = $wpdb->prepare( "SELECT ID, post_title FROM wp_mgf_posts WHERE post_title = %s ORDER BY ID DESC LIMIT 1",$list_name);
       $results = $wpdb->get_results( $sql,ARRAY_A);
   
       var_dump($results); 	
   
       foreach($results as $result)
       {
       $page_id = $result['ID'];	
       }
   
       echo 'Page id = ' . $page_id;
   
       // Create (or update) the page	
       $plant_list = array(
         'post_ID'      => $page_id,     // 0 for create, existing page id for update
         'post_title'    => $list_name,  // the title of the list
         'post_content'  => $content,   // page content
         'post_status'   => 'publish',
         'post_author'   => 1,
         'post_type'   => 'page',
         'post_category' => array(26,30)  // development and plant list categories
       );
       $insert = wp_insert_post( $plant_list );
       ```
   
 * As you can see, I have put in a var_dump and an echo of the retrieved post_id.
   This is the result:
 * array(1) {
    [0]=> array(2) { [“ID”]=> string(4) “7001” [“post_title”]=> string(
   18) “Late Summer Colour” } } Page id = 7001
 *  This looks OK to me. Presumably, I’m doing something wrong but have no idea 
   what. Can anyone help?

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [aamirkhatri](https://wordpress.org/support/users/aamirkhatri/)
 * (@aamirkhatri)
 * [6 years ago](https://wordpress.org/support/topic/problem-with-wp_insert_post-2/#post-12844883)
 * ‘post_ID’ => $page_id,
 * the above should be ID not post_ID.
    -  This reply was modified 6 years ago by [aamirkhatri](https://wordpress.org/support/users/aamirkhatri/).
 *  [corrinarusso](https://wordpress.org/support/users/corrinarusso/)
 * (@corrinarusso)
 * [6 years ago](https://wordpress.org/support/topic/problem-with-wp_insert_post-2/#post-12844990)
 * [@infmgt](https://wordpress.org/support/users/infmgt/)
 * I’ve not use this before, but I am expexting you need a conditional loop in order
   to insert or update :
    [https://developer.wordpress.org/reference/functions/wp_update_post/](https://developer.wordpress.org/reference/functions/wp_update_post/)
 * Altho … now that I google it, you’re right – the docs say anything other than
   0 is an update :
    [https://developer.wordpress.org/reference/functions/wp_insert_post/](https://developer.wordpress.org/reference/functions/wp_insert_post/)
   Lol.
 * You can see the update / insert conditionals here :
    [https://developer.wordpress.org/reference/functions/wp_insert_post/#source](https://developer.wordpress.org/reference/functions/wp_insert_post/#source)
 * Personally, I would use the GUID to check for existing posts. Then fire this 
   method :
    [https://developer.wordpress.org/reference/classes/wpdb/update/](https://developer.wordpress.org/reference/classes/wpdb/update/)
 *  Thread Starter [infmgt](https://wordpress.org/support/users/infmgt/)
 * (@infmgt)
 * [6 years ago](https://wordpress.org/support/topic/problem-with-wp_insert_post-2/#post-12846979)
 * Thanks, Aamirkhatri, that was it. Phew!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Problem with wp_insert_post’ is closed to new replies.

## Tags

 * [wp_insert_post](https://wordpress.org/support/topic-tag/wp_insert_post/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 3 participants
 * Last reply from: [infmgt](https://wordpress.org/support/users/infmgt/)
 * Last activity: [6 years ago](https://wordpress.org/support/topic/problem-with-wp_insert_post-2/#post-12846979)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
