Support

Account

Home Forums Backend Issues (wp-admin) Make Post Object Field Searchable by Dynamically Populating Hidden Text Field

Helping

Make Post Object Field Searchable by Dynamically Populating Hidden Text Field

  • Hello all,

    I have a custom post type for staff members and at the bottom of each page I have a section where the page editor can add a list of staff members using a repeater field with a post object field nested underneath it. This all works well.

    My issue is with the post object field not being searchable. I would like people to be able to use the search to locate the staff members listed on the page. My idea to get around this was to create a hidden field (called searchables) that would be dynamically populated by the contents of the post object field. I just need to grab the name field from each of the post objects and list them in the hidden searchables field.

    To do this I was using the acf/update_value filter to go and get the employee names and put them in the searchables field.

    //Update Searchables Field
    function my_acf_update_value( $value, $post_id, $field ) {

    $value = ”;

    if( have_rows(’employee_group’) ):
    while ( have_rows(’employee_group’) ) :
    the_row();

    $post_objects = get_sub_field(‘group_members’);
    if( $post_objects ):
    foreach( $post_objects as $post):
    setup_postdata($post);
    $name = the_field(’employee_name’). ‘, ‘;
    $value .= $name;
    endforeach;
    wp_reset_postdata();
    endif;

    endwhile;
    else :
    endif;
    return $value;
    }
    add_filter(‘acf/update_value/name=searchables’, ‘my_acf_update_value’, 10, 3);

    I am a bit lost at the moment trying to get name field to show in the hidden field. Also I need to add some code that keeps the update specific to that row so the repeater field can be used to create more than one employee group on the page if needed.

    Any suggestions on how I might do this is greatly appreciated.

    Thanks,
    Ben

  • Hello –
    I have made a little bit of progress on this post. I have been able to get the first of the repeater rows to show the list of names in the searchables field. Any idea why the second repeater row is not adding to the list of names?

    function my_acf_load_value($value, $post_id, $field) {
    	if( have_rows('employee_group') ):
    		while ( have_rows('employee_group') ) : the_row();
    				global $post;
    				$post_objects = get_sub_field('group_members');
    				if( $post_objects ):
    					foreach( $post_objects as $post):
    					setup_postdata($post);
    					
    							$id = get_the_ID();
    							$first = get_field('first_name', $id);
    							$last = get_field('last_name', $id);
    							$name = $first . ' ' . $last . ', ';
    							$names .= $name;
    					endforeach;
    					wp_reset_postdata();
    				endif;
    	endwhile;
    	  $value = $names;
    	else :
    		// no rows found
    	endif; wp_reset_postdata();
    	return $value;
    }
    add_filter('acf/update_value/name=searchables', 'my_acf_load_value', 10, 3);

    Thanks for taking a look at this. Any help is greatly appreciated.

    Ben

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.