Ajax Search Pro Knowledge Base
Buy Ajax Search Pro!DocumentationGet SupportDevelopment
  • Knowledge Base
  • Safe Coding Guideline
  • Hooks
    • Filters
      • Query & Output
        • asp_query_args
        • asp_query_{type}
        • asp_cached_content
        • asp_pre_get_front_filters
        • asp_before_ajax_output
        • asp_shortcode_output
        • asp_print_search_query
      • Keyword Suggestions
      • Search Results
        • asp_suggested_phrases
        • asp_results
        • asp_cpt_results
        • asp_buddyp_results
        • asp_attachment_results
        • asp_comment_results
        • asp_blog_results
        • asp_terms_results
        • asp_peepso_group_results
        • asp_peepso_activities_results
        • asp_only_keyword_results
        • asp_only_non_keyword_results
        • asp_result_groups
      • CSS & JS
        • asp_load_js
        • asp_load_css
        • asp_load_css_js
      • Template & Output
        • asp_icl_t
      • Index Table Related
        • asp_index_on_save_stop
        • asp_indexing_keywords
        • asp_indexing_string_pre_process
        • asp_indexing_string_post_process
        • asp_post_content_before_tokenize_clear
        • asp_post_content_before_tokenize
        • asp_post_excerpt_before_tokenize
        • asp_post_title_before_tokenize
        • asp_file_contents_before_tokenize
        • asp_post_permalink_before_tokenize
        • asp_index_terms
        • asp_post_custom_field_before_tokenize
        • asp_index_cf_contents_before_tokenize
        • asp_index_before_shortcode_execution
        • asp_index_after_shortcode_execution
    • Templating
      • Filter layouts Templating
      • Result Templating
    • Constants
  • Frontend Filters
    • Taxonomy Filters
      • Restricting results to the same category as the current post object
      • How to automatically check/select filter values based on the archive page?
    • Frontend filters API
  • Tips & Miscellaneous
    • Divi
      • Divi Blogs Live Search and Filter
    • Jet Engine
      • Jet Engine Listing Grid Live Search and Filter
      • Searching Jet Engine Custom Meta Storage fields
    • Tutorials
      • PDF results thumbnails
      • Demo setup: Staff search and Filter
      • Demo Setup: WooCommerce Search
      • Demo setup: WooCommerce Shop Search and Filter
      • Demo Setup: Events Search – Events Manager
      • Demo Setup: Events Search – The Events Calendar
      • Compact ‘pop-out’ search bar placement on specific pages only
      • Index Table – Indexing ACF repeater field titles and contents
      • Change Suggested Phrases conditionally
      • How to add shortcode to the results content?
      • How to add variables to the “redirect to url” or the “show more url”?
      • Indexing Shortcodes within custom field contents
    • Post Types
      • Index Table - Indexing child post contents to parent
      • Limit results to specific post IDs only
      • Filter posts (or CPT) which user can’t access
      • Restricting results by user Groups using the Groups plugin by itthinx
      • Limiting results to specific posts by parent ID
      • Limit results to current page children
      • Excluding posts or CPT by parent ID(s)
      • Searching posts, pages (or any CPT) by specified keywords only, nothing else
      • Showing the post type name in result title or content
      • Searching within given categories/taxonomy terms only
      • Search only in the same category as the current post or page (single page)
      • Search only within the current category (or any taxonomy) archive
      • Filtering pages by page template
    • Taxonomy Terms
      • Displaying taxonomy name in taxonomy term results
      • Limiting taxonomy term results to specific term IDs only
    • WooCommerce
      • Displaying On Sale products only in WooCommerce
      • Ordering product by stock status
      • Making a product in-stock & out of stock filter
      • Add to cart button for Vertical and Horizontal results
      • Get formatted price in result title or in content
      • Showing products in-stock only
      • Showing in-stock and backorder products only
      • Excluding hidden catalog products
      • Showing featured products first
      • How to search products & product SKU?
      • How to search Products by variation SKUs?
      • Search product attributes
      • How to search users by city, state, zip code… ?
    • Compatibility
      • WPML Compatibility issues and fixes
    • Files & Media
      • Searching Image EXIF metadata
    • Other
      • Restrict results from the current author archive page
      • Presetting search options via a custom URL
      • Matomo analytics tracking integration
      • Replace search keywords (whole words)
      • Replace or remove characters from search phrase
      • Adding spaces in search phrase between alphabetics and numbers
      • How to change the results URL to something else?
      • Numbering the results
      • How to use the search without the live ajax feature, as a regular search?
      • Empty search input field on the search results page
      • Singular and Plural keywords index
  • Other
    • SearchQuery
    • REST API
    • Building a custom REST API
    • Javascript API
    • Javascript Hooks
      • asp_redirect_url
      • asp_search_data
      • asp_live_load_html
      • asp_search_html
      • asp_compact_width
    • Theme Functions
Powered by GitBook
On this page
  1. Tips & Miscellaneous
  2. Tutorials

Index Table – Indexing ACF repeater field titles and contents

PreviousCompact ‘pop-out’ search bar placement on specific pages onlyNextChange Suggested Phrases conditionally

Last updated 1 year ago

Using this custom code, you can enable indexing of repeater field post titles and/or contents for the current post. Change the following parameters in the $arguments variable in the first part of 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

  • 'run_shortcodes' -> true or false, to run the shortcodes in the content fields

add_filter(
	'asp_index_cf_contents_before_tokenize', // do not change this
	function ( $content, $post ) {

		// Change these arguments to your needs
		$arguments = array(
			'field_names'    => 'field1, field2', // Comma separated list of field names to index
			'index_title'    => true,     // To index the related post title
			'index_content'  => true, // To index the related post content
			'run_shortcodes' => true, // To run shortcodes within Flex element contents
		);

		// --------------------- WARNING: DO NOT CHANGE ANYTHING BELOW -------------------------
		$o = new class($arguments, $content, $post) {
			/**
			 * @var string
			 */
			private string $field_names = 'field1, field2';
			/**
			 * @var bool
			 */
			private bool $index_title = true;
			/**
			 * @var bool
			 */
			private bool $index_content = true;
			/**
			 * @var bool
			 */
			private bool $run_shortcodes = true;

			/**
			 * @var string
			 */
			private string $content;

			/**
			 * @var WP_Post
			 */
			private WP_Post $post;

			/**
			 * @param array<string, mixed> $args
			 */
			public function __construct( array $args, $content, $post ) {
				$this->field_names    = $args['field_names'] ?? $this->field_names;
				$this->index_title    = $args['index_title'] ?? $this->index_title;
				$this->index_content  = $args['index_content'] ?? $this->index_content;
				$this->run_shortcodes = $args['run_shortcodes'] ?? $this->run_shortcodes;
				$this->content        = $content;
				$this->post           = $post;
			}

			/**
			 * @param mixed $any
			 * @param int   $level
			 * @return string
			 */
			private function anyToString( $any, int $level = 0 ): string {
				$str = '';
				if ( is_array( $any ) ) {
					foreach ( $any as $sub_arr ) {
						$str .= ' ' . $this->anyToString( $sub_arr, $level + 1 );
					}
				} elseif ( !is_object($any) ) {
					$str = (string) $any;
				}

				return $str;
			}

			public function fetchRepeater(): string {
				$content = $this->content;
				$post    = $this->post;
				if ( function_exists('get_field') ) {

					$fn_arr = explode(',', $this->field_names);
					foreach ( $fn_arr as $field_name ) {
						$field_name = trim($field_name);
						if ( empty($field_name) ) {
							continue;
						}

						$args     = array(
							'p'         => $post->ID, // ID of a page, post, or custom type
							'post_type' => 'any',
						);
						$my_query = new WP_Query( $args );
						while ( $my_query->have_posts() ) :
							$my_query->the_post();
							$items = get_field($field_name);
							if ( empty($items) ) {
								continue;
							}
							if ( is_array($items) ) {
								foreach ( $items as $item ) {
									if ( isset($item->ID) ) {
										if ( $this->index_title ) {
											$content .= ' ' . get_the_title($item->ID);
										}
										if ( $this->index_content ) {
											$content .= ' ' . apply_filters('the_content', get_post_field('post_content', $item->ID));
										}
									} elseif ( is_array($item) || is_string($item) ) {
										if ( $this->run_shortcodes ) {
											$content .= ' ' . do_shortcode( $this->anyToString($item));
										} else {
											$content .= ' ' . $this->anyToString($item);
										}
									}
								}
							} elseif ( is_string($items) ) {
								$content .= ' ' . $items;
							}

						endwhile;
					}
				}
				return $content;
			}
		};

		return $o->fetchRepeater();
	},
	10,
	2
);
Advanced Custom Fields
What is this, and where do I put this custom code?