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

#36319
zisis19zisis19
Participant
add_filter('asp_results', 'asp_remove_attachments_where_post_type', 10, 1);
function asp_remove_attachments_where_post_type( $results ) {
	$included = array(
	'newsroom'
	);

	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), $included) ) {
					return $results;
				}
			}
		}
	}
	unset($results);
}

This did the trick. Instead of exluding an array I included it… Don’t know why the other way round didn’t work… I am just happy that it works now. Thank you for the initial code 🙂 .

PS: In the Plugin settings I removed any restrictions I had tried out regarding CPTs or Custom Fields.

PS2: One last question. Is it possible to limit this code only for one specific search instance? I named this one Dokumentenfinder as it searches only for dokuments. But what if I need to create a new search bar that will search for pages, posts etc… ? I created a second search instance for this as a test and I see that the above code applies to this new search instance as well. When I remove it, the 2nd one works just as it should… So, how can we limit this code only to the “Dokumentenfinder” search instance? It would be a shame not to be able to create more search instances in the future if needed :/.