It just won't insert anything, if I echo it, it will show everything fine, but it won't insert anything in database. The table name is correct. Code -

global $wpdb;
$meta2 = get_post_meta($post->ID, 'customFields', true); 
$metas2 = explode(",", $meta2); 
foreach ($meta2 as $meta) 
$wpdb->query( $wpdb->prepare("INSERT INTO customfields(values) VALUES('$meta')")); 

The code is inside Theme functions.php file.

2 Answers 2


Consider trying with $wpdb->insert() method instead of raw query. Using of functions/methods is recommended for interacting with database over raw requests, unless absolutely impossible.


You really don't even need to use the $wpdb class. update_post_meta() will work great in this situation.

I'm assuming your exploding a comma separated list of numbers and want to insert them back as an array.

$meta_key = 'customFields';
$item = $post->ID;

$numbers = get_post_meta( $item, $meta_key, true);
$array = explode(",", $numbers );

for ( $i=0; $i < count( $array ); $i++ ) {

    update_post_meta( $item, $meta_key[$i], $numbers[$i] );


This will insert an array in the custom field that looks like this:

customFields[0] => first_item,  
customFields[1] => second_item

and so on...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.