WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm hitting the wall for nearly two hours now, trying to figure this out.

Here is the thing' I have made a xml parser to read xml file and add posts (with content) from xml:

$ch = curl_init( "http://abc.xml" );

curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
$data = curl_exec( $ch );
curl_close( $ch );
$doc = new SimpleXmlElement( $data, LIBXML_NOCDATA );

$size = count( $doc );
$count_added = 0;
for( $i = 0; $i <= 1; $i++ )
    $img = $doc->prod[$i]->awImage;
    // skipping the part of post adding
    $my_post_id = wp_insert_post( $new_post );
    update_post_meta( $my_post_id, 'prod_image', $img );

Problem is that custom filed doesn't get my $img value, even though if I do echo $img - I get the content... Strange thing is if I create some other variable $var="test" and enter this instead $img in update_post_meta - the custom field gets the value of $test...

Whats wrong here, because I'm starting to hit keyboard really badly...


share|improve this question
Have you tried to set $img variable directly (I mean write it down by hand) to "$doc->prod[$i]->awImage" content and check if it is stored? – Ján Bočínec Sep 6 '11 at 12:57
You should also check if the post ID isn't already present, when inserting. – kaiser Sep 6 '11 at 13:35
up vote 1 down vote accepted

Maybe if you use add_post_meta instead, because update_post_meta assumes that you have already that value and you are updating it ( from http://codex.wordpress.org/Function_Reference/update_post_meta ).

Also check if $doc->prod[$i]->awImage is actually a string (do print_r instead of echo).

share|improve this answer
If a value isn't found, update_post_meta will insert it. "The first thing this function will do is make sure that $meta_key already exists on $post_id. If it does not, add_post_meta($post_id, $meta_key, $meta_value) is called instead and its result is returned." From the codex page you linked to. – chrisguitarguy Sep 6 '11 at 16:47
Fair enough, maybe it's the loop value... what if dumps print_r($doc) to see if the problem is there? – Álex Acuña Viera Sep 6 '11 at 18:16
thanks for the suggestion of print_r. the thing was this var was somehow SimpleXMLElement object and wordpress had problems with that, so what i did was - $img=strval($img); to return string from this value. – kroma Sep 7 '11 at 7:14

Your Answer


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.