Prevent duplicate records in plugin table

I'm working on a plugin that keeps track of like counts on every post in the site. When the plugin is activated a c

I'm working on a plugin that keeps track of like counts on every post in the site. When the plugin is activated a custom table is created to store all the data. The plugin then checks on every page load if a record exists in the table for that particular post id. The code looks like this:

function mpc_generate_html($content)
{
    global $wpdb;
    $obj = get_queried_object();
    $post_id = $obj->ID;


    $count = $wpdb->get_var("SELECT COUNT(*) FROM $table_name WHERE post_id = $post_id");

    if ($count == 0) {
        $wpdb->insert(sswc_table_name, array('post_id' => $post_id));
    }
    //More code here.
}

If the record does not exists for that post ID, it $wpdb->insert() that record into the table. Now the plugin is working fine on my local environment with like 3 posts. But in product its creating multiple records for the same postId which is not what i want. What am I doing wrong?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742307893a4419305.html

相关推荐

  • Prevent duplicate records in plugin table

    I'm working on a plugin that keeps track of like counts on every post in the site. When the plugin is activated a c

    14小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信