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

Reply To: PDF search results

#40737
Ernest MarcinkoErnest Marcinko
Keymaster

Hi,

1. Try this variation:

add_filter( 'asp_results', 'asp_attachment_parent_in_category', 10, 1 );
function asp_attachment_parent_in_category( $results ) {
  // Category IDs
  $categories = array(1, 2, 3);
  
  foreach ($results as $k=>&$r) {
	if ( $r->content_type == 'attachment' ) {
		$parent_id = wp_get_post_parent_id( $r->id );
		if ( !is_wp_error($parent_id) && !empty($parent_id) ) {
			if ( $parent_id == 0 || !in_category($categories, $parent_id) ) {
				unset($results[$k]);
			}			
		} else {
			unset($results[$k]);
		}
	}
  } 
  return $results;
}

2. That is bad news, this may not be possible without theme modifications then. Maybe a bit more aggressive code will do it, try this:

add_filter( 'asp_regular_search_result', 'asp_force_guids', 10, 1 );
function asp_force_guids( $r ) {
	if ( isset($r->asp_data) ) {
		if ( $r->post_type == 'attachment' ) {
			$parent_id = wp_get_post_parent_id( $r->id );
			if ( !is_wp_error($parent_id) ) {
				$r->guid = get_permalink($parent_id);		
			}
		} else {
			$r->guid = $r->asp_data->link;
		}
		$r->post_excerpt = $r->asp_data->content;
		$r->post_content = $r->asp_data->content;
	}
	return $r;
}