Generate list of posts on a page, but fill shortcode values from ACF fields on that page

I have actually found a really elegant solution to this myself.

The issue is not with the [random-posts-list], it is with the script used to generate the [county] shortcode.

Instead of using $post_ID = get_the_ID(); I should have been using $post_ID = get_queried_object_id();. This is a built-in WordPress query.

With the above amend, I also needed to tweak how the ACF field is called on the current page, from $countycode = get_field( 'county_code' ); to $countycode = get_field( 'county_code', $post_ID );, which will then reference the above query.

So the amended script for my [county] shortcode is:

// Register [county] shortcode
function county_shortcode() {
    $post_ID = get_queried_object_id(); // Gets the ID of the final page
    $parentpost_id = wp_get_post_parent_id( $post_ID );
    $countycode = get_field( 'county_code', $post_ID ); // References final page ID from above
    $countycodeparent = get_field( 'county_code', $parentpost_id );
    $countycodedefault = get_field( 'county_code', 'option' );
    if(!empty ($countycode)){
        $countycoderesult = $countycode;
    }
    elseif(!empty ($countycodeparent)){
        $countycoderesult = $countycodeparent;
    }
    else{
        $countycoderesult = $countycodedefault;
    }
    return $countycoderesult;
}
add_shortcode( 'county', 'county_shortcode' );

The advantage of this method is that it will work when my shortcodes are used in other areas, like testimonial plugins or Reusable Content Blocks etc.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)