You have got a few things mixed up.
First, your query checks for time_created
, which does not exist in your table.
Second, you try to insert the date as a integer
using %d
. You may have to treat it as a string.
Finally, you have to set $wpdb
to global in your function
function postviews_add_ip () {
global $post, $wpdb; // Database set to global
$id = $post->ID;
$ip = $_SERVER['REMOTE_ADDR'];
$time = date("Y-m-d H:i:s");
$wpdb->insert(
'wp_postviews_ips',
array(
'postid' => $id,
'ip' => $ip,
'time' => $time
), // added a colon
array(
'%d',
'%s',
'%s' // treat it as a string
)
);
$sql = "DELETE FROM wp_postviews_ips WHERE time < (NOW() - INTERVAL 1 DAY)"; //changed rowname
$wpdb->query($sql);
}
Placement of the code and structure
I would strongly recommend to divide the functions of insertion and deletion. You could use wp-cron
for deletion, so you do not have to call this function all the time. The function itself should work this way – but please be sure to optimze it 🙂