0

With Advanced Custom Fields (ACF) I added a 'relationship field' to the pages that can select posts from a custom post type. The chosen posts will be come part of the page. Searching with wordpress search function will not find the page with info from the included elements.

For example if i search 'safety' i will not find the page but the costum post named safety. I know how to disable the custom post types from search. How would i be able to find the page?

I hope my example is clear?

example of relationship field and output on page

1 Answer 1

1
+50

The problem is that by default WordPress only looks in the title and content field. As you build your pages from multiple pieces of external post types the default query doesn't find those parts.

Before we go any further I want you to know that this is going to be quite involved as you need to modify the search query quite a lot. I therefore also can't code it up for you at the moment, but I'll try to outline the process one would need to take to achieve this the best I can:

What you need to do is modify the search query in a way that it doesn't only search title and content but also linked custom post type posts selected for that page. The relation of ACF is stored as a custom field linked to the post. The value then relates to a custom post.

There are quite some questions on how to modify search queries here one WPSE. Here is just one that might help: Extending search query with additional $sentence value

There are some plugins out there that are able to search custom fields but this goes one step further. But have a look at their code as it might be a good starting point:

In case you opt for SearchWP then searchwp_extra_metadata would probably be the right place to plug this in.

Also have a look at Creating a Search Page in the Codex.

Having said all of this in case the fact that these parts are reusable for various pages isn't essential you might consider just adding these segments as custom fields directly to the page instead of in another custom post type. As such the plugins I listed above would work out of the box and you'd save a lot of time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.