First of all, in order to make this work, you must hack into the core file wp-includes/link-template.php. Find $posts_in_ex_cats_sql = “AND tt.taxonomy = ‘category'”; and change it to: $posts_in_ex_cats_sql = ”; It’s not that we can’t create a filter, it’s just too troublesome to do so, trust me. Also, this filter is designed for ppl who don’t have excluded_categories, if you have, tell me and I will provide another filter.
Okay, we need 1 filter, assuming that you have already been familiar with creating plugin, I will just post the filter, ok?
function get_custom_adjacent_post_join($join, $in_same_cat)
{
global $post, $wpdb;
// we don't process empty post and default post types
if (empty($post) || in_array($post->post_type, array('post', 'page', 'attachment', 'revision', 'nav_menu_item')))
return $join;
// here we specify custom post types and their corresponding taxonomies
$taxonomies = array(
'your_custom_post_type' => 'your_taxonomy'
);
$current_taxonomy = $taxonomies[$post->post_type];
if ($in_same_cat) {
$join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
if ( $in_same_cat ) {
$cat_array = wp_get_object_terms($post->ID, $current_taxonomy, array('fields' => 'ids'));
$join .= " AND tt.taxonomy = '$current_taxonomy' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
}
}
return $join;
}
In somewhere of your plugin, type:
add_filter(‘get_previous_post_join’, ‘get_custom_adjacent_post_join’, 10, 2);
add_filter(‘get_next_post_join’, ‘get_custom_adjacent_post_join’, 10, 2);
Please note this line:
$taxonomies = array(‘your_custom_post_type’ => ‘your_taxonomy’);
If you would like a movie post type and a Director as taxonomy, you would change it to:
$taxonomies = array(‘movie’ => ‘director’);
In case you need to add more, simply add another item for that array.
Hope that will help!