Ask your WordPress questions! Pay money and get answers fast! (more info)

Previous post link with multiple custom taxonomies WordPress

  • CLOSED

I am using previous_post_link and next_post_link to navigate between posts in the same custom taxonomy. The problem I am having is that I need to navigate between posts that have the same two custom taxonomies.

For example on a product page I would like to go to the next product that was the same type and brand. Nike and T-shirt for example.

This is the code I am using at the moment but it is obviously just using one of the custom taxonomies

<?php previous_post_link( '%link', 'Previous Rug', TRUE, ' ', 'collection' ); ?>

Answers (2)

2017-03-09

Jayaram Y answers:

You can do something like this


$thisindex = array_search($post->ID, $ids);
$previd = $ids[$thisindex-1];
$nextid = $ids[$thisindex+1];
if ( !empty($previd) ) {
echo '<a rel="prev" href="' . get_permalink($previd). '">previous</a>';
}
if ( !empty($nextid) ) {
echo '<a rel="next" href="' . get_permalink($nextid). '">next</a>';
}


#2 solution
This goes in functions.php

function adjacent_post_by_category( $category, $direction, $content ) {

// Info
$postIDs = array();
$currentPost = get_the_ID();

// Get posts based on category
$postArray = get_posts( array(

'category' => $category,
'orderby'=>'post_date',
'order'=> 'DESC'
) );

// Get post IDs
foreach ( $postArray as $thepost ):
$postIDs[] = $thepost->ID;
endforeach;

// Get prev and next post ID
$currentIndex = array_search( $currentPost, $postIDs );
$prevID = $postIDs[ $currentIndex - 1 ];
$nextID = $postIDs[ $currentIndex + 1 ];

// Return information
if( $direction == 'next' AND $nextID ):
return '<a rel="next" href="' . get_permalink( $nextID ) . '">' . $content . '</a>';
elseif( $direction == 'prev' AND $prevID ):
return '<a rel="prev" href="' . get_permalink( $prevID ) . '">' . $content . '</a>';
else:
return false;
endif;
}

and where you want to call, you can use this

<?php echo adjacent_post_by_category( 10, 'prev', 'Show previous post' ); ?>

<?php echo adjacent_post_by_category( 10, 'next', 'Show next post' ); ?>


johnw6761 comments:

There are two taxonomies that need to match they are called 'collection' and 'size'. The next and previous links need to go to posts that have the same collection and size as the current post.

2017-03-10

mod mi answers:

I guess since you use the previous post link you are using this inside a single custom post type template? Please check this free plugin "Next/Previous Post Link Plus for Wordpress". It adds the functionality you describe among with other extensive options. http://www.ambrosite.com/plugins/next-previous-post-link-plus-for-wordpress

There is an "in_cats" option where you can specify either categories or taxonomy IDs. Check this part from the plugins options' documentation:

in_cats
Specifies the category IDs to include in the next/previous links; also works with custom taxonomies. Separate multiple category/taxonomy IDs with commas. This parameter overrides both in_same_cat and in_same_tax — if you list any category IDs here, the next/previous links for all posts will be drawn only from those categories you specify.

<?php next_post_link_plus( array('in_cats' => '3,5') ); ?>


johnw6761 comments:

Thanks but it doesnt look this plugin can look at two seperate taxonomies


mod mi comments:

It seems there is an option for that, please check my improved answer


johnw6761 comments:

It allows you to select multiple terms from a SINGLE taxonomy I need to select from MULTIPLE taxonomies for example:

Brand = Nike and Product = TShirt


mod mi comments:

'in-cats" accepts more than one taxonomy term ID.