How to get ACF field on the different pages

Retrieving ACF fields outside the loop

ACF’s get_field() function works inside the WordPress loop without extra arguments, but when you need to read a field value from functions.php, an AJAX handler, or a template for a different post, you have to pass the post ID explicitly.

Problem: How do you retrieve an ACF field value from any context, including functions.php?

Solution: To retrieve an ACF field from any context — including functions.php — pass the page ID as the second argument: get_field( 'some_field', $page_id ). Let's look at a practical example: reading ACF fields inside an AJAX callback.

Step 1. On the page that initiates the AJAX request, expose the page ID to JavaScript:

<script>
    // Get the current page ID.
    // Outside the loop, use: $wp_query->get_queried_object_id()
    var current_page_id = '<?php echo $post->ID; ?>';
</script>

Step 2. Pass it as an AJAX parameter:

var data = {
    // ...other params...
    'page_ID': current_page_id
};

Step 3. In the AJAX handler in functions.php, use the ID to read the field:

$page_id = absint( $_POST['page_ID'] );
$example = get_field( 'example', $page_id );

Note: To get the front page ID, use get_option( 'page_on_front' ).

NOTE: get_field() with a post ID works for any post type. For ACF options page fields use get_field( 'field_name', 'option' ). Outside of AJAX, use get_queried_object_id() to get the current page ID reliably when the global $post is not set.