Home Forums General Issues Post Object Automatic Detection


Post Object Automatic Detection

  • Hello,

    I’m making a theme for a photographer, and have set it so that when he adds a photo it will create a Custom Post in Advanced Custom Fields.

    The problem is that although the “Image Object” is populated, it doesn’t actually show the post on the page until you physically click “Update” on the photo page.

    Is there a way to automatically update the post so that the Image Object gets generated without having to do this?

    Here’s my code at the moment:

    add_action('add_attachment', 'auto_post_after_image_upload'); // WordPress Hook
    function auto_post_after_image_upload($attachId)
        $attachment = get_post($attachId);
        $image = wp_get_attachment_image_src( $attachId, 'large');
        $image_tag = '<p><img src="'.$image[0].'" /></p>';
        $field_name = "album";
        $forSale = "for sale";
        $string = $attachment->post_title;
        $album = current(explode("_", $string));
        $value = $album;
        $photo = substr($string, strpos($string, "_") + 1);
        $postData = array(
    	'post_title' => $photo,
            'post_type' => 'photo',
            'post_category' => array('0'),
            'post_status' => 'publish',
        $post_id = wp_insert_post($postData);
        // attach media to post
            'ID' => $attachId,
            'post_parent' => $post_id
        update_post_meta( $post_id, 'image', $attachId );
        update_field( $field_name, $value, $post_id );
        update_field( $forSale, true, $post_id );
  • Turns out using the field key solves the problem, so if anybody runs into anything similar try using that instead.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Post Object Automatic Detection’ is closed to new replies.