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.

link|improve this question
feedback

2 Answers

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.

link|improve this answer
feedback

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...
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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