Title: Quirk with $wpdb-&gt;insert; multiple inserts
Last modified: August 19, 2016

---

# Quirk with $wpdb->insert; multiple inserts

 *  [filesofnerds](https://wordpress.org/support/users/filesofnerds/)
 * (@filesofnerds)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/quirk-with-wpdb-gtinsert-multiple-inserts/)
 * **This is the last line of my function (barring the end of the block): **
 *     ```
       return $wpdb->insert( $wpdb->postmeta, array( 'post_id' => $post_id, 'meta_key' => '_articleViews', 'meta_value' => 1 ) );
       ```
   
 * It’s inserting not only for $post_id but additionally ($post_id+n).
 * So I get an additional row for instance:
 * meta_id, post_id, meta_key, meta_value
    1, 10, _articleViews, 1 2, 10+n, _articleViews,
   1
 * Any idea why this is happening? I’ve tried WP’s `add_post_meta()` and a raw SQL`
   INSERT INTO`, and I get the same result every time.
 * I’m running the function as an action which is hooked onto “wp_head”. The strange
   thing is that when I hook the action to “pre_get_post” the function or SQL query
   inserts THREE entries, but one has no post ID (post_id = 0).
 * My estimation is that the function is being hooked twice, or it’s being pseudo-
   hooked to the additional post, and the additional post is being loaded.
 * It’s somewhat akin to the way that you can call a single post’s `$post` data 
   on a category page. The thing that really stumps me is why the hook would be 
   called twice on a single page. It seems that my single pages in fact load two`
   $post`‘s data, but the one with the relevant ID is loaded after the hook handlers
   are invoked.
 * But what’s more, even if I write
 *     ```
       <?php
       $post_id = $post->ID;
       $wpdb->insert( $wpdb->postmeta, array( 'post_id' => $post_id, 'meta_key' => '_articleViews', 'meta_value' => 1 ) );
       ?>
       ```
   
 * after `get_footer()`, I get the same effect.
 * Bonkers.

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

 *  Thread Starter [filesofnerds](https://wordpress.org/support/users/filesofnerds/)
 * (@filesofnerds)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/quirk-with-wpdb-gtinsert-multiple-inserts/#post-1395249)
 * Moreover, PHP_debug only shows one query:
 * `[ ] [ W3_Db->query ] [ 1.4550685882568 seconds ] INSERT INTO wp_1_postmeta (
   post_id, meta_key, meta_value) VALUES ('7095','_articleViews','1')`
 * …
 *  [bungeebones](https://wordpress.org/support/users/bungeebones/)
 * (@bungeebones)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/quirk-with-wpdb-gtinsert-multiple-inserts/#post-1395264)
 * Hi,
    I had a similar problem. Maybe this will help
 *     ```
       function write_duplicate_post_to_sale_item_page($data) {
       unset($num_rows);
       $my_ancestor = get_post_ancestors($data );
   
       if($my_ancestor[0]!= 0){
       $my_ancestor_post = get_post( $my_ancestor[0], ARRAY_A );
   
       $send_ID = $my_ancestor[0];
       $send_title = $my_ancestor_post[post_title];
       if($send_title != ""){
   
       $send_content =  $my_ancestor_post[post_content];
       }
       ```
   
 * I truncated the code, and the details are already fuzzy, but basically it looks
   for the ancestor of the post, gets its ID and title and content from there first.
 *  If there is no ancestor YET, that means there is no “published” item yet so 
   you do an sql INSERT but if there is already an item with the ancestors number
   then do an UPDATE of the ancestor row.

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

The topic ‘Quirk with $wpdb->insert; multiple inserts’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [bungeebones](https://wordpress.org/support/users/bungeebones/)
 * Last activity: [16 years, 3 months ago](https://wordpress.org/support/topic/quirk-with-wpdb-gtinsert-multiple-inserts/#post-1395264)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
