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

Reply To: Search for media and exclude search from media in custom post type

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Search for media and exclude search from media in custom post type Reply To: Search for media and exclude search from media in custom post type

#36323
Ernest MarcinkoErnest Marcinko
Keymaster

No problem, I too have long workdays, at the end I usually make a lot of mistakes.

You can of course limit the code to a specific search ID:

add_filter('asp_results', 'asp_remove_attachments_where_post_type', 10, 2);
function asp_remove_attachments_where_post_type( $results, $search_id ) {
	
	if ( $search_id == 1 ) {
		$excluded = array(
			'post_type_name1',
			'post_type_name2',
		);

		foreach ( $results as $k => $r ) {
			if ( isset($r->post_type) && $r->post_type == 'attachment' ) {
				$parent_id = wp_get_post_parent_id($r->id);
				if ( $parent_id != 0 ) {
					if ( in_array(get_post_type($parent_id), $excluded) ) {
						unset($results[$k]);
					}
				}
			}
		}
	}
	
	return $results;
}

I hope this works 🙂