Quirk with $wpdb->insert; 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, 1Any idea why this is happening? I’ve tried WP’s
add_post_meta()and a raw SQLINSERT 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
$postdata 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.
The topic ‘Quirk with $wpdb->insert; multiple inserts’ is closed to new replies.