0

I want to get next and previous posts based on a particular category slugs. For example, some posts might have the "sports" category while another has the "tech" category.

I was hoping to plug in a category slug into get_adjacent_post but it doesn't appear that it's working. It looks like it just wants use the slug "category" or maybe custom taxonomies rather than slugs within the "category" taxonomy.

Here's an example of what I was trying to do:

$adj = get_adjacent_post(true, '', true, 'sports');
2

What you call "slugs" here is more appropriately called terms. It is a little confusing with native taxonomies because it turns into tautology: individual categories are terms of the category taxonomy.

So sports and tech are terms of native category taxonomy.

The function arguments operate on taxonomy level, not term level. That is you need to provide a taxonomy slug, such as category, and all of the terms in that taxonomy will be considered for a match.

From arguments point of view one of the options here is to use $excluded_terms argument to exclude everything but the one term you want.

Other than that it's pretty low level function, which directly generates a lot of custom SQL. There are abundant filters for manipulating that part of the process, like get_{$adjacent}_post_join and get_{$adjacent}_post_where, but it's not something I would call easily adjustable.

0

According to the documentation for get_adjacent_post(), the parameters are:

$in_same_term (bool) (Optional) Whether post should be in a same taxonomy term.
Default value: false

$excluded_terms (array|string) (Optional) Array or comma-separated list of excluded term IDs.
Default value: ''

$previous (bool) (Optional) Whether to retrieve previous post.
Default value: true

$taxonomy (string) (Optional) Taxonomy, if $in_same_term is true.
Default value: 'category'

The $taxonomy parameter is so you can specify which taxonomy (category, tag, or custom taxonomy) you'd like use to select the adjacent post.

So no, it appears you can't specify the taxonomy term (eg, sports or tech in your question).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.