Set Specific Category in Search Bar on a Specific Page

In some cases if you are writing a page or blog post referencing a specific category on your site, you may want to pre-set the selected category in your search bar, to do this you can add the below snippet using the Code Snippets plugin. 

add_filter( 'geodir_search_output_to_main_categories', '_my_set_cat_search', 8, 3 );
function _my_set_cat_search( $html, $cf, $post_type ){
	global $post;
	
	if( is_page() && isset($post->ID) ){ // use is_page() for pages and is_single() for blog posts
		if( $post->ID == 1469 ){// page ID
			$_REQUEST['spost_category'][] = 3; // category ID
		}elseif( $post->ID == 1234 ){ // page ID
			$_REQUEST['spost_category'][] = 9; // category ID
		}
	}
	
	return $html;
}
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us