Support

Account

Home Forums Backend Issues (wp-admin) Set WordPress excerpt and post thumbnail based on custom field Reply To: Set WordPress excerpt and post thumbnail based on custom field

  • Thanks, you’ve been incredibly helpful! Granted my code (which I pasted below) may or may not make you and others cringe in horror, I managed to make it work somehow (I’ve put together a function that handles featured images and custom excerpt one).

    Now, I did a lot of testing and I found that both functions work correctly with the hook “save_post” instead of “acf/save_post“. I’m not sure why, as I’m not sure why if I switch to “acf/save_post“, only the featured image function keeps working (while the excerpt one, “reads” the previous saved value, hence requiring me to save twice). If I then try to lower the priority I receive some internal server errors. But I guess I’m more puzzled at this point as wheter what I accomplished is indeed correct or it’s faulty (and this is the reason it does not work with “acf/save_post”).

    In any way, thanks again for pointing me in the right direction!

    
    add_action('save_post', 'flex_CustomExcerptSetByACF', 50);
    function flex_CustomExcerptSetByACF() {
    
        global $post;
    
        $post_id        = ( $post->ID ); // Current post ID
        $post_excerpt   = get_field( 'post_excerpt', $post_id ); // ACF field
    
        if ( ( $post_id ) AND ( $post_excerpt ) ) {
    
            $post_array     = array(
    
                'ID'            => $post_id,
                'post_excerpt'	=> $post_excerpt
    
            );
    
            remove_action('save_post', 'flex_CustomExcerptSetByACF', 50); // Unhook this function so it doesn't loop infinitely
    
            wp_update_post( $post_array );
    
            add_action( 'save_post', 'flex_CustomExcerptSetByACF', 50); // Re-hook this function
    
        }
    
    }
    
    add_action('save_post', 'flex_FeaturedImageSetByACF', 50);
    
    function flex_FeaturedImageSetByACF() {
    
        $current_screen         = get_current_screen(); // Current admin screen needed to identify the current cpt
        $current_cpt_name       = $current_screen->post_type; // Current cpt name
        $current_cpt_support    = 'thumbnail'; // We want to check if the CPT supports this feature
    
        global $post;
    
        $post_id                = ( $post->ID ); // Current post ID
        $post_image_field       = get_field('post_head_img', $post_id ); // ACF field
    
        if ( ( $post_id ) AND ( $post_image_field ) ) {
    
            $post_image_id          = $post_image_field['id']; // ACF image filed ID
            $post_image_url         = $post_image_field['url']; // ACF image filed URL
    
            // If current cpt supports thumbnails/featured images
    
            if ( post_type_supports( $current_cpt_name, $current_cpt_support ) ) {
    
                if ( ( $post_image_url ) AND ( ( $post_image_url ) != ( get_the_post_thumbnail() ) ) ) {
    
                    update_post_meta($post_id, '_thumbnail_id', $post_image_id);
    
                }
    
            }
    
        }
    
    }