Home Forums ACF PRO Making ACF data available to wp_search? Reply To: Making ACF data available to wp_search?

  • @bokis I needed to be able to search these values as well for my plugin Validated Field so that I could check for unique values (the plugin supports different types of uniqueness per field). It actually needed to work for any field that can support multiple values (select, checkbox, relationship, posts, users, etc), plus I’ve previously been asked to support WPML so that’s in there too.

    My solution was to use the acf/update_value and acf/delete_value hooks to process the array values into discrete meta field entries suffixed by type: “__p” for post IDs, “__t” for taxonomy IDs, “__u” for User IDs, and “__x” for everything else. Additionally I am sorting the array values and saving that for comparison as well.

    This means for a set of Post Ids 3, 1, 2 caled “related_posts” it would save:

    #original value (out of order IDs)
    related_posts = array( 3, 1, 2 )
    # sorted value
    related_posts__ps = array( 1, 2, 3 )
    # discrete ID values
    related_posts__p = 1
    related_posts__p = 2
    related_posts__p = 3

    I will be releasing the 2.0 version of the plugin in a few days so you could take advantage of the metadata that it creates, since these are standard meta fields they should be searchable by most search plugins. Or if you just want to peek at the code: