Support

Account

Home Forums General Issues Displaying correct postID content on blog page (home.php)

Solving

Displaying correct postID content on blog page (home.php)

  • Hi there,

    I just saw this topic: https://support.advancedcustomfields.com/forums/topic/acf-fields-not-displaying-on-posts-page/

    It’s essentially what I have. I load custom fields in the blog page (template: home.php), but it’s showing content from the first blog post instead.

    The problem with the solution above is my setup is a lot more complex than this. Looks something like this:

    – home.php refers to flexible-content.php, which contains the loop to check for repeater fields.
    – If the page contains a repeater ‘block’, it will then load /acf/block-hero.php for example, I use about 10-14 various blocks

    So I might use the “Block Hero” on top of the blog, then add the post loop under that.

    So just passing the blog post ID for each ACF field would be tens if not hundreds of fields. Essentially also complicating or breaking the ‘default’ ACF functionality.

    Is there any way to force ACF to get the blog post any other way? I’ve considered wrapping the entire ACF element in some kind of global variable ID, but the page is already returning the correct page ID…? Even querying the object from within the block-hero.php returns the correct blog page ID, but it’s simply not showing any of that content.

    Any ideas?

    Best,
    Dave

  • 
    $page_for_posts = get_option( 'page_for_posts' );
    

    will return the post id of the page for the blog list. Then this ID can be used to get the fields from that page

  • Hi John,

    Thanks, but as explained i’d have to change tens or hundreds of fields in a few dozen files.

    Is there a better way to just force *any* ACF fields between a piece of code to return a certain page – without modifying every individual get/the_field?

  • I just did a quick look and I found this at the beginning of the ACF code that determines the current post ID

    
    $preload = apply_filters( "acf/pre_load_post_id", null, $post_id );
    

    This can be used to determine the post ID, if you return any value it will override ACF looking for the post ID.

    I do not know the specifics of what you need to do off the top of my head… and it could get complicated and there are a number of factors to consider.

    Just as a for instance, you would want to return the value for get_option( 'page_for_posts' ); only if you were not inside the main loop and you actually want to show a field for the current post. I’m not exactly sure without doing a lot of testing how to determine this.

    this might work, but it is only a guess

    
    add_filter('acf/pre_load_post_id', 'your_filter_name_here', 10, 2);
    function your_filter_name_here($value, $post_id) {
      $queried_object = get_queried_object();
      // queried object will be page for posts if it is the blog page
      if (is_a($queried_object, 'WP_Post') && $queried_object->ID == get_option(page_for_posts')) {
        $value = intval(get_option(page_for_posts'));
      }
      return $value;
    }
    

    There is also a filter at the end of the ACF code to determine the post ID that can potentially also be used in some way.

    
    $post_id = apply_filters('acf/validate_post_id', $post_id, $_post_id);
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.