Support

Account

Home Forums Backend Issues (wp-admin) Set image as featured image Reply To: Set image as featured image

  • here my actual code with that i publish posts form an external form ( using ACF ) :

    function generate_post_from_form_submission() {

    // Get the submitted field values
    $post_title = af_get_field( ‘titolo’ );
    $post_content = af_get_field( ‘contenuto’ );

    // Set up a form using the values for post title and content
    // Replace post_type with whatever type of post you want to generate
    $post_data = array(
    ‘post_type’ => ‘libri’,
    ‘post_status’ => ‘publish’,
    ‘post_title’ => $post_title,
    ‘post_content’ => $post_content,
    );

    // Create post with the previously retrieved values
    $post_id = wp_insert_post( $post_data );

    // Save extra_information field directly to custom field on post
    af_save_field( ‘titolo’, $post_id );
    af_save_field( ‘contenuto’, $post_id );
    af_save_field( ‘copertina’, $post_id );
    af_save_field( ‘immagine’, $post_id );
    af_save_field( ‘autore’, $post_id );
    af_save_field( ‘editore’, $post_id );
    af_save_field( ‘codice’, $post_id );
    af_save_field( ‘punto_di_ritiro’, $post_id );
    af_save_field( ‘citta’, $post_id );
    af_save_field( ‘galleria’, $post_id );

    }
    add_action( ‘af/form/submission/key=form_********’, ‘generate_post_from_form_submission’, 10 );

    How can i solve to can have the field “immagine” to insert a featured image?
    I’ve seen your code :

    <?php

    function acf_set_featured_image( $value, $post_id, $field ){

    if($value != ”){
    //Add the value which is the image ID to the _thumbnail_id meta data for the current post
    add_post_meta($post_id, ‘_thumbnail_id’, $value);
    }

    return $value;
    }

    // acf/update_value/name={$field_name} – filter for a specific field based on it’s name
    add_filter(‘acf/update_value/name=cursusfoto’, ‘acf_set_featured_image’, 10, 3);

    ?>

    and ‘ve understood that it must be placed on functions.php ( where there is my code above ) but has not worked so i’m here to ask help .

    Thank You