epl_search_can_show_post_type(): which post types the listing search can show

Description #

Checks whether the listing search may show a post type. It's true when the post type is registered and visitors can view it (is_post_type_viewable()). The listing search and the [listing_advanced] shortcode use it, so a listing type that's switched off, or a private one such as contacts, can't be searched by changing the address. The filter of the same name changes the answer.


Usage #

$can_show = epl_search_can_show_post_type( $post_type );

add_filter( 'epl_search_can_show_post_type', 'my_function', 10, 2 );

Parameters #

Function:

$post_type (string)
Post type name, for example rental.

Returns bool: true when the search may show it.

Filter:

$can_show (bool)
True when the post type is registered and viewable.

$post_type (string)
The post type being checked. It comes from the search request, so check it before you use it.


Where it's used #

  • The listing search (Listing Search Widget and [listing_search], action=epl_search): each post type in the request is checked and the ones that fail are dropped. If none are left, the search finds nothing.
  • [listing_advanced] post_type attribute: registered post types that fail are left out. A post type that isn't registered (such as a listing type that's switched off) stays and finds nothing, as before. If none are left, the shortcode finds nothing.

Examples #

Show a link to search rentals only when the rental type is switched on:

if ( epl_search_can_show_post_type( 'rental' ) ) {
	$url = add_query_arg(
		array(
			'action'    => 'epl_search',
			'post_type' => 'rental',
		),
		home_url( '/' )
	);
	echo '<a href="' . esc_url( $url ) . '">Search rentals</a>';
}

Any viewable post type passes by default, including posts and pages. Limit the listing search to EPL listing types:

function my_epl_search_listing_types_only( $can_show, $post_type ) {
	return $can_show && in_array( $post_type, epl_all_post_types(), true );
}
add_filter( 'epl_search_can_show_post_type', 'my_epl_search_listing_types_only', 10, 2 );

Source #

lib/widgets/widget-functions.php, in epl_search_can_show_post_type(). Used by EPL_Search::set_post_type() in lib/includes/class-epl-search.php and EPL_Advanced_Shortcode_Listing::set_post_type() in lib/shortcodes/class-epl-advanced-shortcode-listing.php. Since 3.6.