Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Search posts with same taxonomy as visited post
This topic contains 5 replies, has 2 voices, and was last updated by kyrian 2 months ago.
- AuthorPosts
- January 28, 2023 at 9:35 am #41123
Hi,
looking at the settings I don’t think this is currently possible, but it would be interesting to have an option to auto-filter the search results dynamically depending on the category / tag of the visited post.
This could be used to show a “related posts” module for instance.Is this something you plan to add in the future?
Thanks
January 28, 2023 at 10:10 am #41126Hi,
This is already possible via the plugin API, check this knowledge base.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
January 28, 2023 at 10:21 am #41129Thanks a lot!
January 28, 2023 at 10:26 am #41130So after customizing this code in placing it in function.php, then I create a custom search form. But how do I tell this form to use the provided code to only search in the same taxonomy as the visited page?
This last step is not clear to me, sorry.January 28, 2023 at 10:53 am #41131By 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
Best,category
taxonomy.
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
January 28, 2023 at 11:17 am #41132now it’s clear, thanks
- AuthorPosts
You must be logged in to reply to this topic.