I’m looking to return both the question and the answer in a custom search overlay. For instance, if a customer types in “flavors”, I would like a dynamically populated “card” that has both the question and answer in it, for instance — Q: What flavors do you have? A: Strawberry, Chocolate, etc.
I have the Javascript / card stuff taken care of, I just can’t seem to return both the Q and A which match a search term in the API.
Yeah this is way out of my wheelhouse, and if you’re not sure how, I think I’ll just put a pin in this. It was worth the exploration. Thanks a lot for your assistance, John!
Hey John,
It’s a Repeater Field. Check out the structure here:
You’ll see the nested repeater, etc.
No problem! So, I was just playing around with this, and if I grab the fields on the parent field, I get All Fields and Sub Fields of the FAQ page.
if (get_post_type() == 'page') {
array_push($results['pages'], array(
'title' => get_the_title(),
'link' => get_the_permalink(),
'faq' => get_field('faq-topic'),
));
}
Here’s an example of a response:
"faqs": [
{
"faq_group": {
"question": "What are your shop hours?",
"answer": "<p><strong>Our shop hours:</strong></p>\n<ul>\n<li>Monday – Thursday: 10-6 PM EST</li>\n<li>Friday: 10-5 PM EST</li>\n<li>Saturday: Closed</li>\n<li>Sunday: Closed</li>\n</ul>\n",
"linked_page": ""
}
}],
Where I’m really trying to go from here is to access both the question
and answer
and access them via javascript template literal, which I have working really well. All other post types and their respective fields are working nicely. I’m just struggling with this one.
Testing the search queries for keywords unique to the FAQ questions/answers fields return the FAQ page, which I assume means that the plugin is working. If the search query is extending to a repeater field, it’s returning the parent page on which those fields are outputted.
I’m confused on where to go next, and I appreciate your patience.
The code in my original post is in a file called search-route.php. I’m assuming the filter you’re talking about in the article is the following:
/**
* Modify the search query with posts_where
*
* http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
*/
function cf_search_where( $where ) {
global $pagenow, $wpdb;
if ( is_search() ) {
$where = preg_replace(
"/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
"(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
}
return $where;
}
add_filter( 'posts_where', 'cf_search_where' );
Am I correct in thinking that this is the function I need to add to my search-route.php?
Thanks, John – I have a plugin installed ‘Better ACF Search’ to get the fields, and that seems to work as if I search for a unique keyword in one of the FAQ fields, I get the FAQ Page as a search result. I’m assuming that plugin is working at this because of that.
My question now becomes, if I search in meta_key LIKE "faq_%"
, do I need a new WP_Query for those results, or can I amend my original query?
I appreciate the help, I’m a little new at this and learning.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.