I would like to put pagination on each single product page in WooCommerce so that a user can move between products in that category easier than having to go back out to the main category page every time.
I know it’s possible to use the standard WordPress pagination links likeâ¦
<?php previous_post_link('« %link'); ?>
<?php next_post_link('%link »'); ?>
This works if I want to page through all products, but I only want to page through products that are within the category I’m in. Does anyone know how I can limit this so products outside this category aren’t included?
I’ve tried using the in_same_term parameter as mentioned in the WordPress codex to get the links only showing if the next/prev product is in the same category, but it’s returning an integer for some reason. Here’s the code I’m usingâ¦
<?php next_post_link( '%link', '%title', TRUE, '' ); ?>
This returns nothing at all even though it follows the Codex structure. I’ve also triedâ¦
<?php next_post_link( '%link %title', TRUE, '' ); ?>
And this is what I’m getting in returnâ¦
1 %title
I’m stumped where to go next.
Here is a function that I have recently written that also does the job and is quite flexible
THE IDEA:
You first need to get the current post id, which I get through
get_queried_object_id()
. The post ID will be used to retrieve:The post terms the post belongs to with
wp_get_post_terms()
. To speed things up, only the ID’s of the terms will be returned. The first ID will be used (you can modify the code here to decide which term will be used if a post have more than one term) and this will be used to retrieve all the posts which has this certain termThe post ID’s of the posts that is directly adjacent to this post to determine and retrieve the next and previous post from this one
All the info above will be used in a
tax_query
withget_posts
to retrieve all the posts that shares the term from the current post. In the function, the default taxonomy iscategory
and thepost_type
is set to any to get all the posts that has this specific termAgain, to make the code faster and to safe on resources, we only going to get the post ID’s as this is all that is needed
Now comes the important parts of the code. We now need to determine the following:
The current position of the current post in the returned array of post ID’s from the custom
get_posts
query. The function used here isarray_search
If there is a post before or after this post (next or previous posts, the definitions are the same as for the build in functions
next_post_link()
andprevious_post_link()
), get the ID’s of these postsUse the ID’s with
get_post
to retrieve the next and previous post’s titles from the current postLastly will be to return the links. I have set messages if the current post is either the first or last post in the array and there are no next or previous post. You can decide what you want to do here, and for all that matters, the rest of the code
To make the code even faster and more efficient, I have made use of the Transient API which you can read further on. I have also used the
transition_post_status
action hook to hook a function to delete these transients whenever the post status of a post change. This includes new posts being published, post being updated and post deleted/undeletedTHE CODE:
Here is the code. This goes into your functions.php
HOW TO USE:
You can now use the code as follows in you single.php. The default taxonomy is
category
and post type isany
. If your custom taxonomy is calledmytax
, you can use the code like this