This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: ACF not showing in API

#45716
Ernest MarcinkoErnest Marcinko
Keymaster

Hi Tom,

I’m afraid that will not work that way. Selecting the fields to earch for will not get the field values. There might be a way to do it via custom code, but I am not sure:

add_filter( 'asp_results', 'asp_custom_meta_to_wpjson', 10, 4 );
function asp_custom_meta_to_wpjson( $results, $search_id, $is_ajax, $args ) {

    if ( $args['is_wp_json'] ) {
		foreach ($results as $k=>&$r) {
			if ( isset($r->post_type) && $r->post_type == $post_type ) {
				// Declare the fields explicitly
				$r->my_field1 = get_field('my_field1', $r->id);
				$r->my_field2 = get_field('my_field2', $r->id);
			}
		}
	}

    return $results;
}

After this they should be popping up in the the $post->asp_data->my_field1 fields and so on..