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

Worpdress Multiple taxonomy with next_post_link WordPress


I created two custom taxonomy (produit_type and produit_marque). I correctly displays the post list of each taxonomy (in taxonomy-produit_type.php and taxonomy-produit_marque.php). My problem comes when I view the page single-produit.php which corresponds to a product in one of the selected taxonomy. I shall post the good product but I will wish 2 things:

=> Display a list of the same product type (5 at random from the same class and with the same value). If I have the taxonomy 'produit_type' whose value is 'chair' I must show only products whose taxonomy is 'produit_type' and the value is 'chair'. Knowing that a product can belong to several produit_type (eg 'light', 'accessory and decorative').

=> Have a simple paging where I post a link to the previous one post or the post next (if any) of the same taxonomy and taxonomy of the same value. As before I will be able to navigate between posts having for example taxonomy 'produit_type' and value 'chair', knowing also that the post may have more value for the taxonomy produit_type.

In the two case I'm supposed to know where the person click, on a menu produit_type (eg, lamps, chairs, accessories and deco), then click on a product from the list of this taxonomy, I will show Product with this information, 5 products mini (described above) and a previous one link and another product next product.

My problem here is that when I get on the single-page produit.php I am unable to know what I come taxonomy, since if the product belongs to several produit_type I am not able to find if I clicked on one or the other to come to the single page.

You can find the dev site here : [[LINK href="http://test.5francs.fr"]]5francs[[/LINK]]

Thank you in advance for your help!

DDD

Answers (1)

2012-12-11

John Cotton answers:

For your first question:


// First, get the terms for the current post
global $post;
$terms = wp_get_object_terms( array( $post->ID ), 'produit_type', array( 'fields' => 'slugs') );

// Do a custom query for random other posts of the same taxonomy terms
$five_random = new WP_Query(
array(
'post_type' => 'any',
'orderby' => 'rand',
'posts_per_page'=> '5',
'post__not_in' => array( $post->ID ), // exclude the current post
'tax_query' =>
array( array(
'taxonomy' => 'produit_type',
'field' => 'slug',
'terms' => $terms // get posts with the same terms
))));

// And loop through to output as you wish
if( $five_random->have_posts() ) {
// ..etc
}


For your second question - what is are the next/previous links? If you are sorting by date (like a blog) then next/previous makes sense. But if you've got a list of furniture like yours, what defines "next" or "previous?

Once you've established that, then a custom query like the code above should do it.