Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

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.

share|improve this answer
add comment

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...
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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