Advanced Custom Fields (ACF) and their Javascript API

You are trying to fetch a custom post – you cannot do that with ACF javascript API directly. ACF js api is to get acf fields for a post.

What you need is the wp-rest-api with restAPI enabled for the ACF field that you want to query by – in your case it is ‘osm_geometry’.

Check out these examples here:
https://www.advancedcustomfields.com/resources/wp-rest-api-integration/


If you are not so keen about using ACF’s rest API, you can register a simple restAPI yourself and write the working PHP code there and then call your restAPI from javascript.

Here’s how :

function custom_get_post_data( $request ) {
    $osm_geometry = $request->get_param( 'osm_geometry' );

    $args = array(
        'numberposts'   => -1,
        'post_type'     => 'mountains',
        'meta_key'      => 'osm_geometry',
        'meta_value'    => $osm_geometry
    );

    $the_query = new WP_Query( $args ); 
    
    // Do more logics here... and create your final data
    
    $final_data="";

    // Return the data as a JSON response
    return new WP_REST_Response( $final_data );
}

function custom_restapi_for_acf_field() {
    register_rest_route( 'custom/v1', '/post-meta/(?P<osm_geometry>\d+)', array(
        'methods'  => 'GET', //or POST
        'callback' => 'custom_get_post_data',
    ) );
}

add_action( 'rest_api_init', 'custom_restapi_for_acf_field' );

You can add more fields in the restAPI as you want.

tech