Support

Account

Forum Replies Created

  • After some debugging, I found that acf_set_featured_image function isn’t running.

    add_filter(‘acf/update_value/name=character_featured_image’, ‘acf_set_featured_image’, 10, 3);
    I suspect my acf/update_value isn’t working here for some reason.

    Ok, I was wrong here. I was using “Query Monitor” plugin to debug which didn’t properly show the result. The issue was $post_id was returning block id instead of post id.

    So corrected code will be

    function acf_set_featured_image( $value, $post_id, $field  ){
        if($value != ''){
    		$post_id = get_the_ID() ? get_the_ID() : $_POST['post_id'];
    	    //Add the value which is the image ID to the _thumbnail_id meta data for the current post
    		update_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=character_featured_image', 'acf_set_featured_image', 10, 3);

    or

    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
    		update_post_meta(false, '_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=character_featured_image', 'acf_set_featured_image', 10, 3);
  • After some debugging, I found that acf_set_featured_image function isn’t running.

    add_filter('acf/update_value/name=character_featured_image', 'acf_set_featured_image', 10, 3);
    I suspect my acf/update_value isn’t working here for some reason.

  • I am trying to follow the method described here (https://support.advancedcustomfields.com/forums/topic/set-image-as-featured-image). But It’s not working whatever I do. There is no change in the featured image. Note, I am in a custom post type.

    This is added to my themes functions.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
    	    update_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=character_featured_image', 'acf_set_featured_image', 10, 3);

    This is my field group.
    https://ibb.co/XJ2wJJv

  • Answered here. https://github.com/AdvancedCustomFields/acf/issues/269#issuecomment-731647793

    Please keep in mind that some WP functions such as get_the_title() and get_the_post_thumbnail_url() refer to a global $post object if the $post_id variable is not provided (or in your case set to null). During the AJAX request to render the block preview, that global $post object is not setup, which explains why those functions are not running as intended.

    The soution is simple, either setup the gobal $post object manually, or use the provided $post_id parameter in any get_the_title() or get_the_post_thumbnail_url() calls 👍.

  • <?php
    $text = get_field('character_overview_title');
    if (emtpy($text)) {
      $text = get_the_title();
    }
    ?>
    
    <div class="character-overview" id="overview">
        <h2><?php echo $text; ?> Overview</h2>
        <p class="overview-description"><?php the_field( 'character_overview_description' ); ?></p>
    </div>

    Breaks the site for some reason.

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