Home › Forums › Pre-purchase Questions › External search
Hello,
I’d like to know if in the Extended version it’s possible to create a field that searches a term in an external database and displays a few suggestions while the post author is typing it. Sorta like the search box for categories does while you’re writing a new post, but in my case the search should be done on an external source. Then the post author would choose one option from the list returned by that search and the field would point to an external page.
I need this because the data in this field will come from a big database that isn’t connected to WordPress and it’s unfeasible to create new categories for each page it points to, as the database is constantly updated.
+1 I have the same question.
I need a field that query an external API with after the user has enter a minimal search (3 letters for example), send the query each time the search change and return a list of options that the user can select : key as object_id and name as label.
Thank you
I cannot give you any code except for a few snippets, but it is possible.
First you create select field with the following options
Choices = dynamically generated
Stylized UI = Yes
Use AJAX to lazy load choices = Yes
Create an acf/prepre_field filter to dynamically generate the choices. The reason why we do this on acf/prepare_field is so that the choices are only loaded when used. Connect to your external DB and populate the choices from there.
Create an acf/load_field filter for the same field. This filter should only run during AJAX requests. At the top of the function add:
if (!defined('DOING_AJAX') || !DOING_AJAX) {
return $field;
}
Other than this it does the same thing that the first function does and you can just call that function and return what it returns.
And then create and acf/format_value filter that will use the selected value to get what you need from the external DB and format it in whatever way it needs to be formatted.
There may be other ways to do this, but I had to do something similar. The main difference was I was not connecting to an external DB.
You must be logged in to reply to this topic.
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!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 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.