Support

Account

Home Forums General Issues Images Problems On Edit Post Form

Solved

Images Problems On Edit Post Form

  • Hey Everyone,
    Lately I’ve been having this issue of images not attaching to posts when they are submitted via edit post form.
    When a user creates a post everything is ok, but when he wants to replace those images the problem I mentioned above occurs.
    I managed to fix the problem on gallery images and plain image fields, But when it comes to “Feature Image” field – not so much.
    It seems that replacing the featured image of a post is impossible.
    This is the code:

    add_action( 'template_redirect', 'set_post_featured_image');
    function set_post_featured_image() {
    
    /*checks the url so it wont be fired on every template redirect, the query is set by the form on submit*/
    if(isset($_GET['profile_change'])){ 
    
    //url query contains the post id
    $the_current_post = $_GET['profile_change'];
    
    //this returns null 
    $featured_image = $_POST['acf']['field_607d6236e63ff'];
    
    //this two works ok
    $logo = get_field('company_logo', $the_current_post);
    $cover_photo = get_field('תמונת_רקע', $the_current_post);
    
    $data = array (
        //$featured_image,
        $logo,
        $cover_photo
    );
    
    foreach($data as $item):
        $update_post = array(
    
    //the return of the image fields are already ID
            'ID' => $item,
            'post_parent' => $the_current_post
    
        );
       wp_update_post($update_post);
       endforeach;
      }
    }

    I tried to use acf/save_post action but it just wouldn’t fire it no matter what I did.

    btw… when i am checking the $_POST request im getting a redirect a nonce, a message and an element (maybe that is the problem, im not getting the acf field values which just updated?)

    Thank you

  • This will be a string value and may not update the parent correctly if not an integer.

    
    $the_current_post = $_GET['profile_change'];
    

    Note that I’m not 100% sure of this but it is always safe to make sure it is an integer

    
    $the_current_post = intval($_GET['profile_change']);
    

    These 2 lines could be returning image object instead of IDs, what you you have the return values set to?

    
    $logo = get_field('company_logo', $the_current_post);
    $cover_photo = get_field('תמונת_רקע', $the_current_post);
    

    if you want to ensure that they return the ID no matter what the return value is set to then set formatting of the value to false and also insure that they are integers.

    
    $logo = intval(get_field('company_logo', $the_current_post, false));
    $cover_photo = intval(get_field('תמונת_רקע', $the_current_post, false));
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.