Created
January 12, 2015 14:58
-
-
Save roborourke/df3999e05f135c05e821 to your computer and use it in GitHub Desktop.
WordPress enable prev/next link for CPT / Taxonomy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Enables $in_same_cat for any taxonomy | |
add_filter( 'get_previous_post_join', 'taxonomical_prev_next', 10, 3 ); | |
add_filter( 'get_previous_post_where', 'taxonomical_prev_next', 10, 3 ); | |
add_filter( 'get_next_post_join', 'taxonomical_prev_next', 10, 3 ); | |
add_filter( 'get_next_post_where', 'taxonomical_prev_next', 10, 3 ); | |
function taxonomical_prev_next( $sql, $in_same_cat, $excluded_categories ) { | |
global $post; | |
if ( $post->post_type != 'post' && $in_same_cat ) { | |
$taxonomies = get_object_taxonomies( $post->post_type ); | |
$cat_array = wp_get_object_terms($post->ID, $taxonomies, array('fields' => 'ids')); | |
if ( count( $cat_array ) ) | |
$sql = str_replace( "tt.term_id IN ()", "tt.term_id IN (". implode( ",", $cat_array ) . ") ", $sql ); | |
else | |
$sql = str_replace( "AND tt.term_id IN ()", "", $sql ); | |
$sql = str_replace( "tt.taxonomy = 'category'", "tt.taxonomy IN ('" . implode( "','", $taxonomies ) . "') ", $sql ); | |
} | |
return $sql; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment