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

Reply To: Search posts with same taxonomy as visited post

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Search posts with same taxonomy as visited post Reply To: Search posts with same taxonomy as visited post

#41131
Ernest MarcinkoErnest Marcinko
Keymaster

By default the code applies to all search bars. If you only need to apply to a specific search ID, then you can use the $sear_id variable to compare, like this:

add_filter( 'asp_query_args', 'asp_posts_from_same_cat', 10, 2 );
function asp_posts_from_same_cat($args, $search_id) {
	if ( $search_id == 1 ) {
		$taxonomy = 'category'; // Enter the taxonomy name

		// Do not change anything below
		$categories = wp_get_post_terms( $args['_page_id'], $taxonomy, array('fields' => 'ids') );
		if ( !is_wp_error($categories) && count($categories) ) {
			$args['post_tax_filter'][] = array(
			  'taxonomy'  => $taxonomy,    // taxonomy name
			  'include'   => $categories,   // array of taxonomy term IDs to include
			  'exclude'   => array(),
			  'allow_empty' => false        // allow (empty) items with no connection to any of the taxonomy terms filter
			);
		}
	}
	return $args;
}

In this case the code will apply for the search ID=1, and for the category taxonomy.