How to Search WooCommerce Products by ACF Relational Fields

ACF Post Object and Relationship fields link one product to another, and Advanced Woo Search PRO writes the ID and title of each attached item into its index. This guide shows how to index and search them, so a search for a printer model returns the cartridges linked to it.

A customer types in the model number of their printer. They want the cartridge that fits it, and you stock it, but its product page never mentions that model. Nothing comes back.

If you use Advanced Custom Fields to link a part to the models it fits, that relationship is already in your database. WooCommerce search never looks at it, because it reads the product title, excerpt and content and nothing else.

This guide covers how to index the ACF fields that hold those links, and what ends up searchable once you do. By the end you will have:

  • Products findable by what they are linked to. Searching the name of a product or page you attached brings up the products pointing at it.
  • Relationships that maintain themselves. The link you already set in ACF is the search data, so there is no second list to keep in step.
  • One field, many ways in. Attach several items to a product and it becomes findable by any of their names.
Searching a model name returns the parts that reference it
Searching a model name returns the parts that reference it

ACF support is an Advanced Woo Search PRO feature, so the steps below need the PRO version.

What ACF Relational Fields Are

ACF groups its field types, and the Relational group holds pointers to other things on the site rather than text you typed. Two of them link a product to other posts, pages or products: Post Object and Relationship. Those are the two Advanced Woo Search can search through.

Enable one of them for indexing and for search, and the plugin writes the ID and title of every attached item into its index, against the product holding the link. A shopper searching that title then finds the product pointing at it.

The Relational field types in ACF
The Relational field types in ACF

If you have not made any ACF data searchable yet, start with ACF search in WooCommerce, which covers plain text fields.

How to Make ACF Relational Fields Searchable

With these steps in place, a search for the name of any item you attached to a product will return that product.

Follow these steps to make a relational field searchable:

  1. Install and activate Advanced Woo Search PRO, then go to Adv. Woo Search → Updates and activate your license key.

  2. Create the relational field in ACF and attach it to your products. Use Post Object to point at one or more posts, pages or products, or Relationship for a two-way link between them. These are the two field types this works with.

    Then fill it in on the products themselves. An empty field indexes nothing, so this is the step that decides whether any of it works.

    A relational field filled in on a product
    A relational field filled in on a product
  3. Go to the Index config page, find the Data to index option and enable Custom Fields.

    Then click the gear icon next to it. Every custom field on the site is listed and all of them are off by default, so find your ACF field and switch it on.

    Enabling custom fields for indexing
    Enabling custom fields for indexing
  4. Go to the main Adv. Woo Search settings page and click Reindex table. A field that was not indexed before only enters the index on a rebuild, so let it finish, and on a large catalog that takes a while.

    Reindex Table button
    Reindex Table button
  5. Go to the Search Config page, find the Search in option and enable Custom Fields there as well. Click its gear icon and switch on the same ACF field.

    This is the switch people miss. Indexing puts the data in the table, this decides whether a search actually looks at it.

    Enabling custom fields for searching
    Enabling custom fields for searching
  6. Save, then test it. Searching for one of the attached items now finds the product you attached it to.

    Searching an attached item finds the product it belongs to
    Searching an attached item finds the product it belongs to

What Goes Into the Index

Both field types are indexed the same way. What differs is how you attach things in the first place:

Field typeWhat you attachWhat is indexed
Post ObjectOne or more posts, pages or products, picked on the product itselfThe ID and title of each attached item
RelationshipA two-way link between posts, which ACF can keep in step on both sidesThe ID and title of each related post

The link only works in the direction you set it. Attaching a printer to a cartridge with a Post Object field makes the cartridge findable by the printer name, not the reverse. Use a Relationship field if you want both products to find each other, since ACF can write the link on both posts.

FAQs

Do I need the PRO version for this?

Yes. ACF support as a whole is a PRO feature, including indexing relational fields. The ACF support documentation lists everything the integration covers.

Which ACF field types can be searched?

Advanced Woo Search indexes a wide range of ACF fields, including Text, Text Area, Number, Email, URL and Wysiwyg Editor. For linking one product to another, the two that matter are Post Object and Relationship, which is what this guide covers.

Can I search by other fields of the attached item, not just its title?

Yes, for the Relationship field, from version 3.73. By default a search only matches the ID and title of an attached item. The aws_acf_relationship_post_fields filter changes which of its fields are searchable, and takes either WP_Post properties such as post_content, post_excerpt and post_name, or custom field keys such as _sku.

add_filter( 'aws_acf_relationship_post_fields', function( $fields ) {
    $fields[] = '_sku';
    return $fields;
} );

With that in place, searching the SKU of an attached item finds the product it is attached to. Rebuild the index afterwards, since this changes what gets stored. Post Object fields only ever match on ID and title.

Does it work for fields inside a Repeater or Group?

Yes. Fields nested inside Group, Repeater or Flexible Content layout fields are found as well, so a repeating list of compatible models works the same as a single field.

Do I have to reindex every time I change a link?

A full reindex is needed when you first enable a field for indexing, because the data was not in the table before. After that the index is maintained as products are saved.

Does the attached item show up in the results as well?

No. The link only surfaces the product holding the field. Searching a printer model returns the cartridges that have it attached, not the printer itself, because the relationship never pulls the attached item into the results.

Can I show ACF field values in the search results?

Yes, with a code snippet. The aws_excerpt_search_result filter lets you append any ACF field value to a result row, so a part number or a spec can sit under the product name instead of only being searchable. There is a working example in the ACF support documentation.

Can I filter search results by a relational field as well?

Yes, with a separate feature. Results filters can be built on any ACF field, which is covered in search results filters based on ACF fields.

Conclusion

The relationships between your products are usually already mapped out. They sit in ACF fields nobody searches, doing nothing for the customer who types in a model number and gets an empty page.

Two switches and a reindex change that, and the data looks after itself from then on. See the ACF support documentation for the full integration reference, or searching custom fields for the wider picture.

For Advanced Woo Search
Michael Barinov

Michael Barinov

I'm Michael — the developer behind KramaKit. I've spent 12+ years building tools for WooCommerce, trusted by 80,000+ stores. I work alone on purpose: it keeps things focused, and if you've ever emailed support, you've talked to me.

← Previous Advanced Woo Labels: What’s New in Versions 2.47-2.53

Take a look around. Or just say hi.

Browse the full plugin lineup, or send a question - we read every message and reply personally.