If you’ve ever found yourself wanting to modify the post titles in an ACF relationship field, you may have already stumbled across the acf/fields/relationship/result
filter. It’s a pretty useful filter when you want to provide some more contextual information next to a post title in the relationship field results list.
As with many filters built into Advanced Custom Fields, it provides some useful variations that allow us to target specific fields by either their field name or their field key.
This is just one simple example of what we can pull off using this filter and some of WordPress’ built-in utility functions. Keep in mind that I’m targeting a specific field by name (related_posts
) here:
With the above snippet in place, you’ll end up with something like the following in the WordPress admin:
What’s really nice about this filter is that you’ll have access to the WP_Post
object as well as the ACF field array so you have a lot of information at your fingertips to format those post titles in the most sensible way possible.
I definitely recommend checking out the ACF documentation on this filter to learn more.