If you use Custom Post Types with custom taxonomies in WP, you’ve no doubt wanted to add Next & Previous buttons on the
single-cake.php template that keep the same taxonomy term as the page they sit on.
As a loose example, let’s use cake.
cake is our CPT, and we have a custom taxonomy of
flavor. We have a few terms there of
We’re looking at a chocolate cake, and we want a button at the bottom of the
single-cake.php template that says Next Chocolate Cake. So we need a way to link to the next cake that’s also a chocolate cake.
$prev_post = get_previous_post(true, false, 'flavor');
$next_post = get_next_post(true, false, 'flavor');
truemeans we want the next/prev post to be of the same term (‘flavor’ in our case)
falsemeans we don’t want to exclude any ID’s here
flavoris our taxonomy
In the past, I’d have some something with a
WP_Query loop, but now I know this exusts, I don’t need to!