Support

Account

Home Forums ACF PRO Making ACF data available to wp_search?

Solving

Making ACF data available to wp_search?

  • Hello, is there a way to make data from my custom fields accessible to the built in WordPress search?

    For instance I have a 2 level nested repeater set up on a CPT.

    Example of hierarchy:
    Products(CPT)
    – product_group (r)
    — product_item (r)
    — item_sku
    — item_description
    — item_size
    — item_upc

    How would I make every product item’s sub_fields available to the built in WP Search?

    This plug-in is a game changer for me. It allows me to easily adapt to client requests and build just about anything I need or that is requested.

    The only missing piece is search integration using my custom data.

    Thanks in advance,

  • Hello,

    I used this plugin to authorise searching in custom post meta https://wordpress.org/plugins/search-everything/ but i don’t know it’s it works with acf pro, i know it works great with acf normal version.

    Bye

  • @ludovicfauchet
    Thank you for the suggestion, unfortunately this plugin does not go deep enough in my case.

    Although I have tried another plug-in with a similar function that adds more granularity to what is extended to the system as searchable content.
    http://bestwebsoft.com/plugin/custom-fields-search/

    This plugin exposes custom fields set by AFC to the WordPress system search. It looks like the way sub_fields are stored are not global per-say; instead these are stored in a serialized manor inside of it’s global parent AFC field.

    Example:
    If I wanted every SKU available from every product item, regardless of it’s product group. The sub_field for product_group/product_item/item_sku is only available via a serialized instance/variant of the global product_group AFC field.

    Data available to custom field search plugin:
    Product_group
    Product_group_0_
    Product_group_0_group_description
    Product_group_0_group_image
    Product_group_0_group_title
    Product_group_0_prodcut_item
    Product_group_0_prodcut_item_0_item_description
    Product_group_0_prodcut_item_0_item_images
    Product_group_0_prodcut_item_0_item_sku
    Product_group_0_prodcut_item_0_item_upc

    As you can see any given product item’s data is only available if I directly target/choose to add the serialized product_group_#/product_item_# to the list.

    Ideally it would be nice to have sub_field data exposed just like the parent fields, not a serialized instance of a parent.

    Current: Product_group_0_prodcut_item_0_item_sku
    Ideal: Product_group_prodcut_item_item_sku

    Making sub_field references available globally regardless of a serialized parent relationship.

    As a sudo fix for my problem,
    I’ve also set up some top level fields for SEO, mainly a meta description and keywords for each page/product. Via the custom field search plugin, I’ve added my SEO fields to it’s list.

    Now when product info is entered into a page, we also copy the SKU and name for each item into the keywords input. Then that data is available to the built in search.

    Please excuse the long post,
    I wanted to be thorough and share my plight on this mater to hopefully gather more incite into the gray area of AFC content & Search integration.

    Thanks,

  • You should consider SearchWP – it can index custom fields and integrates very well with ACF. You can even expand relationship fields within your search index – pretty neat!
    https://searchwp.com/docs/hooks/searchwp_custom_fields/

  • Thanks Dalton, I had a look at this plug-in, but I’m not sure if it will work with sub_fields from repeaters. In my case the content I need searched is in a nested repeater.

    On the SearchWP forum I found an interesting thread, it’s dev looks like he’d like to make it work with AFC repeaters, but doesn’t see any current options.
    https://searchwp.com/support/topic/any-plans-for-a-future-support-for-the-repeater-field-of-advanced-custom-fields/

    It looks like it could work if you use the ‘Any’ value.
    https://searchwp.com/support/topic/would-like-to-include-acf-fields-in-search-results/

    It would be nice to have sub_field references available globally in some way.

    Even better a built in pathway from AFC to WP search.

    Imagine a checkbox on a AFC field type creation form, “Make Searchable”, you check that on the stuff you want available, and Bam your custom content will show up in search results.

  • Hi traviskelleher,

    Did you find any good solution for this? I also think it would be great with a “Make Searchable” checkbox!

    Kind regards

    Claes

  • This code works for me, no plugin needed:
    https://gist.github.com/charleslouis/5924863
    (put code in functions.php).

  • @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: https://github.com/doublesharp/validated-field-for-acf/blob/master/common/acf_vf_validated_field.php#L191

  • Hi.

    I had a similar problem recently and I found this bit of code that did the trick for me. Here’s a link to the article.

    // Make the search to index custom
    /**
     * Extend WordPress search to include custom fields
     * http://adambalee.com
     *
     * Join posts and postmeta tables
     * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
     */
    function cf_search_join( $join ) {
        global $wpdb;
        if ( is_search() ) {    
            $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
        }
        return $join;
    }
    add_filter('posts_join', 'cf_search_join' );
    
    /**
     * 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' );
    
    /**
     * Prevent duplicates
     * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
     */
    function cf_search_distinct( $where ) {
        global $wpdb;
        if ( is_search() ) {
            return "DISTINCT";
        }
        return $where;
    }
    add_filter( 'posts_distinct', 'cf_search_distinct' );
  • Not sure if this would work for you but Ultimate WP Query Search Filter has a nice search feature (incl. ajax results).

    For checkboxes, you need a work around, as described here.

  • If someone is looking for a solution to this problem is to recommend the plugin:
    https://wordpress.org/plugins/acf-better-search/

    This plugin adds to default WordPress search engine the ability to search by content from selected fields of Advanced Custom Fields plugin.

    Everything works automatically, no need to add any additional code.

  • Thanks nyla! This did the trick!

  • nyla’s solution works almost perfectly. I’m just not getting results for Post Object titles.

    For example, if a page has a Post Object field with a Post titled “Today”, doing a search for “Today” doesn’t return the page. However, if I search for the ID of the post, the page is output in the search results.

    Even though the “Return Format” for the Post Object is Post Object and not Post ID.
    Shouldn’t the Post Title get picked up?
    Does anyone have an idea?

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

You must be logged in to reply to this topic.