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.

I am creating a form with the goal of creating a post and assigning it a feature image. I have the post creation working 100% as expected, but I don't understand why update_post_meta is not functioning as expected.

Here is my code:

if ( $new_post_id = wp_insert_post( array ( "post_title" => $title , "post_content" => $content , 'post_status' => 'publish' ) ) ) {
    $result = add_post_meta( $new_post_id , ‘_thumbnail_id’ , 2978 ); // This is to show how both of these don't function as expected
    $result2 = update_post_meta( $new_post_id , ‘_thumbnail_id’ , 3013 );

Both $result and $result2 have a value, and when I execute: var_dump( get_post_meta( $new_post_id ) ) I get this:

array(1) { [""]=> array(2) { [0]=> string(4) "3013" [1]=> string(4) "2978" } }

It looks like _thumbnail_id does not exist in the meta data, but the values I am trying to add are being saved (separately).

I have also done this: var_dump( get_post_meta( 2960 ) ), on a post I made through this form and manually added a feature image (to test what the id I should use for it is), and I was returned this:

array(3) { ["_edit_lock"]=> array(1) { [0]=> string(13) "1347819627:24" } ["_edit_last"]=> array(1) { [0]=> string(2) "24" } ["_thumbnail_id"]=> array(1) { [0]=> string(4) "2978" } }

Does anyone know of a possible explanation for this behaviour?

share|improve this question
your quotes around _thumbnail_id are curly, just delete and retype them. –  jessica Sep 16 '12 at 20:49
add comment

closed as too localized by Rarst Oct 8 '12 at 18:17

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote -1 down vote accepted

I solved the problem, but I think it might help someone in the future:

I replaced this code:

update_post_meta( $new_post_id , ‘_thumbnail_id’ , 3013 );


update_post_meta( $new_post_id , _thumbnail_id , 3013 );

Removing the quotes around _thumbnail_id.

share|improve this answer
if you look closely, the single quotes around your ‘_thumbnail_id’ were 'nice' ones - possibly from copy/paste from some text source - instead of simply straight ones '_thumbnail_id' –  Michael Sep 16 '12 at 20:10
That's a good observation, and absolutely correct. Also, for passing strings in an argument in php, you do not need quotes. It's a pretty powerful language, this is possible because variables typically require a $ in front of them. –  Andrew Jackman Sep 20 '12 at 13:17
I rarely say something like the following in WPSE, but this is horrible, horrible advice. PHP certainly is a loose language, but should not be abused. Proper syntax of strings should have quotes. If no quotes are present for a string sans spaces and special, error logs become littered with notices of undefined constants. It's not warning or fatal territory, but certainly muddies up any debug attempts for other issues. Secondly, strings with spaces and special chars without quotes will cause syntax errors. Last, code written this way will certainly confuse anyone else looking at it. –  Brian Fegter Oct 5 '12 at 2:58
add comment

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