This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: Show document related posts

#39654
Ernest MarcinkoErnest Marcinko
Keymaster

Hi Peter,

Thank you for contacting us!

First, you need to enabled and test the search for media file contents. Here is the full documentation on how you can do that.
Once you get that going, and the media results are appearing in the live results list, then apply the custom code below.

Try adding this code to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.

add_filter('asp_results', 'asp_return_posts_by_attachment_results');
function asp_return_posts_by_attachment_results($results) {
	$ids = array();
	$parent_ids = array();
	$new_results = array();
	// Gather existing post results first
	foreach($results as $k=>&$r){
		if ( isset($r->post_type) ) {
			if ( $r->post_type == 'attachment' ) {
				$parent_ids[] = wp_get_post_parent_id($r->id);
				unset($results[$k]);
			} else {
				$ids[] = $r->id;
			}
		}
	}
	$parent_ids = array_unique(array_diff($parent_ids, $ids, array(0)));
	foreach ( $parent_ids as $id ) {
		$res = new stdClass();
		$res->id = $id;
		$res->title = get_the_title($id);
		$res->content = get_the_content(null, false, $id);
		$res->image = \WPDRMS\ASP\Utils\Post::parseImage($res, array(
				'get_content' => false,
				'image_sources' => array('featured', 'content'),
				'apply_the_content' => true)
		);
		$res->content = wd_substr_at_word(strip_tags($res->content), 130);
		$res->post_type = get_post_type($id);
		$res->link = get_permalink( $id );
		$res->content_type = 'pagepost';
		$new_results[] = $res;
	}
	return array_merge($new_results, $results);
}

This custom code removes the attachments, and replaces them with the posts where they were originally attached to.