I want to write a custom query using $wpdb for a Relationship field in a Field Group on my site. I realize I could hook into the acf/fields/relationship/result filter and run another query there, but I don’t want the original query to be run in the first place.
How can I stop the original query from being run, and replace it with my own query / results?
Could I simply:
remove_action('wp_ajax_acf/fields/relationship/query', array($this, 'ajax_query')); add_action('wp_ajax_acf/fields/relationship/query', 'my_custom_wpdb_query');
And how would I go about removing that action for only one relationship field on a specific form?
No, that won’t work, ACF does not actually do anything on that hook and only provides it so that you can alter the query that’s going to be run anyway.
To replace the query completely you’d need to figure out how to remove the ajax actions that are called, and unfortunately, I don’t see any way to do that….
and then I find one.
So I’ve never really given this much thought, and I cannot tell you everything that is involved, this is just a vague idea of how it could be done.
EDIT: making sure that you return the values for the relationship field exactly the way that ACF returns the values
Actually, it’s not a half bad idea. The relationship field can have some issues with extremely large sites with thousands of posts. Similar issues can be seen with extremely large numbers of terms. These subjects come up from time to time here and replacing the WP query or the get terms calls with a $wpdb query would be a good solution for these.
If I have time I’m going to explore this further.
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!
© 2019 Advanced Custom Fields. Subscribe