WordPress   

How to insert data into WordPress database tables

In this tutorial, we will talk about inserting a new record into an existing table in the WordPress database. The following code snippet will insert a new record for the given 3 fields (post_id, vote, user_id) in the table votes(wp_votes). Remember it will auto-generate values for some fields like ID(Auto increment), TIMESTAMP, etc. If those fields were defined while creating the Table.

global $wpdb;
$table_name = $wpdb->prefix . 'votes'; // Table name
$post_id = 1234; // static value for demonstration purpose only
$vote = 1; // static value for demonstration purpose only
$user_id = get_current_user_id();
$wpdb->insert($table_name, array('post_id' => $post_id, 'vote' => $vote, 'user_id' => $user_id)); 
Need Professional Support for Your Website Problems?

Whether you're facing website issues or struggling with code implementation, our team is here to assist. Hire us to get your website back on track.

Hire Us

Got an idea? We'll turn it into reality.

Tell us what you're building, what's broken, or what numbers need to move. We'll come back within one business day with a short plan — no long decks, no sales calls without purpose.

Response time: First reply within 1 business day on 100% of messages.
Transparent scope: Fixed proposals with weekly check-ins & live task tracking.
Ship-ready builds: Every engagement leaves behind documented, transferable work.
Long-term partners: Ongoing retainers for support, SEO, ads, and content.