Index Table – Indexing ACF repeater field titles and contents
Using this custom code, you can enable indexing of Advanced Custom Fields repeater field post titles and/or contents for the current post. Change the following variables in the code as you need them
  • $field_names -> one or more repeater field names, separated by comma
  • $index_title -> true or false, to index the post titles found in the repeater field
  • $index_content -> true or false, to index the post contents found in the repeater field
1
add_filter('asp_index_cf_contents_before_tokenize', 'asp_post_content_before_add_acf_relationship', 10, 2);
2
function asp_post_content_before_add_acf_relationship($content, $post) {
3
$field_names = 'field1, field2'; // Comma separated list of field names
4
$index_title = true; // To index the related post title
5
$index_content = true; // To index the related post content
6
$run_shortcodes = true; // To run shortcodes within Flex element contents
7
// ----------------------------------------------------------------
8
if ( function_exists('get_field') ) {
9
$fn_arr = explode(',', $field_names);
10
foreach ($fn_arr as $field_name) {
11
$field_name = trim($field_name);
12
if ( empty($field_name) )
13
continue;
14
15
$args = array(
16
'p' => $post->ID, // ID of a page, post, or custom type
17
'post_type' => 'any'
18
);
19
$my_query = new WP_Query( $args );
20
while ( $my_query->have_posts() ) : $my_query->the_post();
21
$items = get_field($field_name);
22
if ( empty($items) )
23
continue;
24
if ( is_array($items) ) {
25
foreach ($items as $item) {
26
if ( isset($item->ID) ) {
27
if ( $index_title ) {
28
$content .= ' ' . get_the_title($item->ID);
29
}
30
if ( $index_content ) {
31
$content .= ' ' . apply_filters('the_content', get_post_field('post_content', $item->ID));
32
}
33
} else if ( is_array($item) || is_string($item) ) {
34
if ( $run_shortcodes )
35
$content .= ' ' . do_shortcode( wd_array_to_string($item));
36
else
37
$content .= ' ' . wd_array_to_string($item);
38
}
39
}
40
} else if ( is_string($items) ) {
41
$content .= ' ' . $items;
42
}
43
44
endwhile;
45
}
46
}
47
return $content;
48
}
Copied!
Last modified 1yr ago
Copy link