'How to access ACF field in a custom Wordpress Rest API endpoint?
I have a Advanced Custom Fields FAQ repeater field that I'm trying to add FAQ's to my custom search results, and am unable to get the field through the Rest API.
I'm registering the endpoint like so:
add_action('rest_api_init', 'register_search');
function register_search() {
register_rest_route('fralin/v1', 'search', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'search_results',
'permission_callback' => '__return_true',
));
}
Which calls the Callback:
function search_results($data) {
$main_query = new WP_Query(array(
'post_type' => array('product', 'post', 'page', 'diagram'), //Would grab FAQ as a custom post type, but it's not it's own custom post type - just custom fields on a page.
'posts_per_page' => 18,
'post_status' => 'publish',
'order_by' => 'relevance',
'order' => 'ASC',
'no_found_rows' => true,
's' => sanitize_text_field($data['term']),
));
$results = array(
'products' => array(),
'pages' => array(),
'posts' => array(),
'diagrams' => array()
);
while ($main_query->have_posts()) {
$main_query->the_post();
global $product;
global $post;
if (get_post_type() == 'product') {
array_push($results['products'], array(
'title' => $product->get_name(),
'link' => get_the_permalink(),
'featuredImage' => get_the_post_thumbnail_url(),
'sku' => $product->get_sku(),
'priceHTML' => $product->get_price_html(),
'tagline' => get_field('product_tagline', get_the_ID()),
'originalDesign' => get_field('original_design', get_the_ID()),
'productAttributes' => array(
get_field('pickup_design', get_the_ID()),
get_field('pickup_appearance', get_the_ID()),
),
));
}
if (get_post_type() == 'post') {
array_push($results['posts'], array(
'title' => get_the_title(),
'link' => get_the_permalink(),
'featuredImage' => get_the_post_thumbnail_url($post->ID, 'thumbnail'),
'publishedDate' => get_the_time('F j, Y'),
));
}
if (get_post_type() == 'page') {
array_push($results['pages'], array(
'title' => get_the_title(),
'link' => get_the_permalink(),
'faq' => get_field('faqs'), // Trying to access FAQ Questions and Answers here.
));
}
if (get_post_type() == 'diagram') {
array_push($results['diagrams'], array(
'title' => get_the_title(),
'thumbnail' => get_field('wiring_diagram_thumbnail')['sizes']['thumbnail'],
'pdf' => get_field('wiring_diagram_pdf'),
'description' => get_field('wiring_diagram_description')
));
}
}
return $results;
}
I see why I'm unable to get access to the FAQ value. It's searching the pages post type for any values search values, and none are there. FAQ's are just added to a page template - not it's own custom post type.
That said, I'm totally lost on how to include ACF fields in search results.
I want a user to search for "ice cream" and have my search function look for FAQ fields involving those keywords, and be able to return the question and answer in the search results. I'm just stuck at actually getting the values.
Any ideas on where to start? Thank you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
