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

Reply To: Show document related posts

#39713
Ernest MarcinkoErnest Marcinko
Keymaster

Thank you!

I made a slight correction to the code, now it seems to be working. I tried to search for a few documents, like “API-desc-guidelines2411” and it seems to be returning only the post now.

For reference, the updated code is:

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 = get_the_post_thumbnail_url($id);
		$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);
}