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've got an add_action in functions.php, the action is for save_post and the function I'm running I've named set_attachment_url. I'm simply getting the first attachment on a post and setting a custom field with the URL of that attachment.

Good news is, everything works great when I'm adding content from the wp-admin. But when I add content from the iOS app, I don't get the same results. I have to add the post, tap the post, click "update" and THEN it works.

I don't know enough about the hooks and when each gets fired, although I'm learning quickly. Can anyone spot what the issue may be, why it's not working from iOS on insert, but on update it will.

Here's the basics ...

add_action( 'save_post', 'set_attachment_url', 10, 1 );

// process the attachment url
function set_attachment_url($post_id){

    // get the post and status
    $my_post = get_post($post_id);
    $post_status = $my_post->post_status;

    // if we are publishing
    if($post_status == 'publish'){

        // check custom field -- this is what tells us if we need to process this photo or not
        // if it does not have a photo_url then we need to work on it

        $photo_url = get_post_meta($post_id, 'photo_url', true);

        // if no length, we need to fill it

            // get the first attachment for this post
            $args = array( 'post_type' => 'attachment', 'numberposts' => 1, 'post_status' => null, 'post_parent' => $post_id ); 
            $attachments = get_posts($args);

            // set the attachment ID and get the photo url
            $attachment_id = $attachments[0]->ID;
            $photo_url = wp_get_attachment_url($attachment_id);

            // update the custom field with the image source
            update_post_meta( $post_id, 'photo_url', $photo_url );

            // set the featured image
            update_post_meta( $post_id, '_thumbnail_id', $attachment_id );



share|improve this question
up vote 1 down vote accepted

the update post function actually calls the insert function so it should work, now I'm not sure but try changing:




since php strlen() returns 0 on an empty string.

share|improve this answer
+1 Thanks for the heads up on empty(), I've got that in place. It's still only working when I update it, which is really strange. I've also tried xmlrpc_publish_post and that does set the featured image, but does not add the photo_url meta. – jyoseph Jul 4 '11 at 17:50

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.