Index Table - Indexing child post contents to parent

This is useful, if you don't want to search and return certain posts (or posts from a certain post type), but you want to index the contents along with the parent post.

Ex. Returning Forum topics only as results, but also indexing all the replies to that topic

The codes below only work with the index table engine. After the code is applied, the index table needs to be re-created for it to apply.

What is this, and where do I put this custom code?

add_filter('asp_post_content_before_tokenize_clear', 'asp_add_children_to_parent', 10, 2);
	function asp_add_children_to_parent($content, $post) {
	$children_array = get_children( array('post_parent' => $post->ID) );

	if ( !is_wp_error($children_array) )
	foreach ( $children_array as $child ) {
		$content .= " ".$child->post_content;
	}

	return $content;
}

Limiting by post type only

add_filter('asp_post_content_before_tokenize_clear', 'asp_add_children_to_parent', 10, 2);
function asp_add_children_to_parent($content, $post) {
	if ( $post->post_type == 'topic' ) {
		$children_array = get_children( array('post_parent' => $post->ID) );
		if ( !is_wp_error($children_array) )
		foreach ( $children_array as $child ) {
			$content .= " ".$child->post_content;
		}
	}
	return $content;
}

Last updated