How to Search Posts by Custom Field in WordPress?

Table of Contents

Are you interested in searching posts using custom field data in WordPress? Custom fields let you add extra info to your posts, pages, products, or custom post types. Allowing users to search using this custom field data can significantly enhance the accuracy and relevance of search results.

With the Search Bar block from the Nexter Blocks, you can easily search any post type based on custom field data.

To check the complete feature overview documentation of the Nexter Blocks Navigation Menu block, click here.

Requirement  – This block is a part of the Nexter Blocks, make sure it’s installed & activated to enjoy all its powers.

To do this, make sure you have created at least one custom field for any post type. You have to use the free Advanced Custom Fields (ACF) to create the custom field.

For instance, we’ll create a custom field called location for normal blog posts.

Then add the Search Bar block on the page or template and follow the steps –

Create Custom Fields

Once you have the ACF plugin installed follow the steps – 

1. From the Dashboard, go to ACF > Field Groups and click on Add New button. 

2. Add a name to the field group.

3. Then add a field type and add a name. For instance, we’ll add a checkbox field with the name called Location.

4. Once done, from the Settings section set the location to Post under Rules.

searchbar custom post type post

5. Then click on the Save Changes button.

6. After that, you have to add value in the custom fields on some posts.

searchbar custom post type post editor

Use the Search Bar Block

Then add the Search Bar block on the page or template.

1. Open the item under Search Bar and select Post from the Source section.

2. From the Select Post dropdown, select Posts.

Now, you should be able to see a dropdown beside the search bar.

Then make the necessary changes to Label, Placeholder and Show Count sections.

3. After that, go to the Standard Search tab and click on the wrench icon of ACF Filters.

4. Then in the ACF Key field, you have to add the custom field name.

To find the field name in ACF, edit the field group there you will find the field name.

searchbar custom post type post location

searchbar acf filters

Now, you’ll be able to search posts by location names.

In a similar way, you can search pages, products, or custom post types by custom field data.

Still in Doubt? Let’s Assist You

Need Help? Ask AI Bot

Have Feedback or Questions?

Join our WordPress Community on Facebook!

Related Docs