330 Locust Dr.
Verona, WI 53593
Powderkeg Web Design
Message Us

A fantastic way to allow searching by ID for Advanced Custom Fields objects

December 19, 2016

Advanced Custom Fields are awesome. They allow you to add an infinite amount of customizations to your WordPress site and are pretty easy to implement. They also have a large amount of hooks and filters you can use to enhance the fields as well.

One of the things I’ve had requested quite a bit by clients is to be able to search for a post by the ID associated with it. Say you have a select ACF field that loads all blog posts on your site. By default, ACF implements the Select2 dynamic dropdown so that it only populates a few and allows for live searching as you type. What if you have many posts that are similarly named and the only real unique thing about them is the ID? Typing in the ID will not bring up the post you desire by default. You need to add a short snippet of code to your functions.php file:

This hooks in to the post_object field type specifically ( you can change that other field types as well ) and checks to see if what you are typing is numeric. This makes the assumption that if you are typing in a number then you are searching for an ID. If it is indeed a number then it adds an additional parameter to the query that is running for the search that checks if there is an exact match to the ID entered. If a match is found, it will return the corresponding post!

Lead Developer

Nick is an ambitious and versatile developer with a wide range of talents. He is an efficient coder that utilizes his resources in order to achieve both his and the clients’ goals. His Alma Mater is the University of Wisconsin – Stevens Point where he graduated with honors in Web and Digital Media Development.