Support

Account

Home Forums General Issues Completely Replace Relationship Query

Solving

Completely Replace Relationship Query

  • 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

  • Thanks for the response – I sort of feared that. It’s okay, I’m just going to replace the relationship field with a WP Advanced Search and write my own AJAX functions to add the posts to the current post’s custom field.

    Thanks again.

  • 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.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Completely Replace Relationship Query’ is closed to new replies.