我也有同样的问题,而且我需要定制帖子类型所需的一切&;自定义分类法。user54318为我指出了正确的方向,因为它无法运行cpt,所以我也将在这里分享我的结果:
//get custom taxonomies.
$terms = wp_get_post_terms( get_the_ID(), \'product_cat\' ); //last argument is the custom taxonomy. change to desired tax
//run through all terms and filter out the one, that i need.
$stay_in = array();
foreach( $terms as $term ) :
/*this loop looks for a category, that is a children of category id 37. change to your needs.
only importance is to build an array of term ids, to be included in the previous/next behaviour, so if you already know your ids, you could also use something like $stay_in = array( 43 ); and skip this whole loop..*/
if ( $term->parent == 37 ) :
$stay_in[] = $term->term_id;
break; //break out the foreach, if found.
endif;
endforeach;
//get all post ids, that are in my defined category
$args = array(
\'post_type\' => \'product\', //custom post type
\'posts_per_page\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'product_cat\', // custom taxonomy
\'field\' => \'term_id\',
\'terms\' => $stay_in,
\'operator\' => \'IN\', //change to your needs.. IN, NOT IN, AND, EXISTS, NOT EXISTS
)
),
\'orderby\' => \'post_date\',
\'order\' => \'ASC\',
\'fields\' => \'ids\', //only return the post ids, not the whole post-objects
);
$all_posts = new WP_Query( $args );
//search for the current post by its id and look for the previous / next ids
$this_index = array_search( $post->ID, $all_posts->posts );
$prev_id = $all_posts->posts[ $this_index - 1 ];
$next_id = $all_posts->posts[ $this_index + 1 ];
//echo links, if prevoius/next exists
if ( ! empty( $prev_id ) ) :
echo \'<a rel="prev" href="\' . get_permalink( $prev_id ) . \'">\' . __( \'previous\', \'your_theme_text_domain\' ) . \'</a>\';
endif;
if ( ! empty( $next_id ) ) :
echo \'<a rel="next" href="\' . get_permalink( $next_id ) . \'">\' . __( \'next\', \'your_theme_text_domain\' ) . \'</a>\';
endif;
wp_reset_postdata();