WordPress.org

Ready to get started?Download WordPress

Forums

add custom field to parent post while adding attachment (1 post)

  1. dirkhaim
    Member
    Posted 6 years ago #

    I hooked to add_attachment, and I want to add custom field to the parent post with the URL of the new attachment.

    I am using this code:

    function twp_attachment_handler($attach_ID)
    {
    	$attach_post_obj = get_post($attach_ID);
    
    	// check if post is not a post, than return
    	$parent_post_obj = get_post($attach_post_obj->post_parent);
    	if ($parent_post_obj->post_type != 'post')
    		return;
    
    	$url = wp_get_attachment_url($attach_ID);
    
    	add_post_meta($attach_post_obj->post_parent, 'full', $url);
    
    	return;
    }

    The problem is that if upload before I save for the first time, the custom field is not added. It's as if the post_parent field is not initialized yet.

    Anything I can do about it? Or maybe I should hook somewhere else?

    BTW, I am using the new v2.3

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic