Hello,
So to start, I’m able to add most of the ACF field values to wp search using Relevanssi. I’m running into one issue that is driving me crazy. I’ll explain.
I have a custom post type called “People”. I’m using all ACF fields for the user to create the person (not using content editor). NOTE – I also have a custom taxonomy for “people” (categories).
However, I don’t want a visitor to be able to view just the individual person post type (using single.php) or there to be an archive page (archive.php). I have the following arguments set for the “People” post type:
public => ‘false’,
has_archive => ‘false’,
exclude_from_search => ‘true’
Ok, so I have a page template called people (because there will multiple pages displaying the people – team, presenters, …etc. ).
In this page template I have a repeater field, with a Post Object sub field to select from the “People” cpt. That will display a persons info on that page (team, preseters, ..etc).
All works great until I get to the search part. Because of my options set when creating the people cpt, if you search someones name, it won’t return any results.
However, I would really like wp search to find the page using the “people” page template where that person appears.
So as an example to help clear up any confusion.
If I create a new person (people cpt), lets call him John Smith. Then on the team page, using the people page template, I select John Smith from the post object field. So that John Smith and his info show up on the team page.
So if a user were to search “John Smith”, it currently comes back with “Sorry, no results”. Even though I don’t want the individual post for John Smith to show up, I would like the team page he appears on to be listed as a result.
If anyone can offer any advice on how to achieve this I would really appreciate it. And I apologize if this is more of WordPress specific question. I was just hoping other ACF Pro users have come across the same issue and found a solution.
The problem is that what you’re searching for is content associated with another post. The only thing that’s on the “people” page are the post IDs of the posts that contain the content. As far as I know, there isn’t a search application of any kind (other than search engines like google) that will search this related content.
What you need to do is to make the content of those people pages part of the post that you want to return results for. For example, let’s say that the title of the person post is their name and you want the name searchable on the people page.
add_action('acf/save_post', 'copy_persons_to_people', 20); // priority 20 runs after acf has saved content
function copy_persons_to_people($post_id) {
// we need to create a new meta key to hold the list of people
// this is the meta key you'll use to set up the search of this field
$meta_key = 'related_peoples';
// clear this field if it exists for this post
// need to start fresh each time
delete_post_meta($post_id, $meta_key);
// see if there are any related people
if (have_rows('repeater_field_name', $post_id)) {
while(have_rows('repeater_field_name', $post_id)) {
the_row();
$person = get_field('sub_field_name');
// assuming the above returns a post object
// add the title of the person post to the post meta of this post
// the last parameter set to false means this meta key can have
// multiple values
add_post_meta($post_id, $meta_key, $person->post_title, false);
}
}
}
Hi John,
Thank you for your speedy reply. I do have to apologize, I just realized I was thinking of another post type when explaining my issue.
On the People page template, instead of a repeater with a post object sub field. I have just a post object field that allows multiple selections. Then in my template file I was using a foreach loop to display each person selected.
I tried to edit your code snippet for my correct situation, but think I’m doing something wrong as it’s still not returning any results. And yes, for the people cpt, the title is what contains the person’s name.
add_action('acf/save_post', 'copy_persons_to_people', 20); // priority 20 runs after acf has saved content
function copy_persons_to_people($post_id) {
// we need to create a new meta key to hold the list of people
// this is the meta key you'll use to set up the search of this field
$meta_key = 'related_peoples';
// clear this field if it exists for this post
// need to start fresh each time
delete_post_meta($post_id, $meta_key);
// see if there are any related people
$persons = get_field('team_members', $post_id);
foreach ( $persons as $person ) {
add_post_meta($post_id, $meta_key, $person->post_title, false);
}
}
On the page template, the acf field for the post object is called ‘team_members’.
Thanks again for your help, I really appreciate it.
You may need to use setup_postdata() and wp_reset_postdata() along with the global $post variable described here https://www.advancedcustomfields.com/resources/post-object/
add_action('acf/save_post', 'copy_persons_to_people', 20); // priority 20 runs after acf has saved content
function copy_persons_to_people($post_id) {
// we need to create a new meta key to hold the list of people
// this is the meta key you'll use to set up the search of this field
$meta_key = 'related_peoples';
// clear this field if it exists for this post
// need to start fresh each time
delete_post_meta($post_id, $meta_key);
// see if there are any related people
$persons = get_field('team_members');
if ($persons) {
global $post;
foreach ($persons as $post) { // need to use $post here
setup_postdata($post);
add_post_meta($post_id, $meta_key, $post->post_title, false);
}
wp_reset_postdata();
}
}
Hi John,
Unfortunately, I’m still not able to get that to work. Probably just going to have to create a people single template file and allow the people cpt to be searchable.
I appreciate your help, and I do really like the concept you provided. I’ll just have to figure out how to make it happen.
Thanks again,
Ryan
John,
Just wanted to let you know that your code did end up working. It wasn’t working when I was building the site locally on MAMP, but once I moved to a remote web server, it started working. I have no idea why it works now and not locally, but I’m just happy it’s working so not going to question it.
Thanks again for all your help, man!
The topic ‘ACF and WP Search’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.