WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

On a single post page I have the following code after my loop:

<?php previous_post_link('&laquo; %link') ?> &#124; 
<?php next_post_link(' %link &raquo;') ?>

For some reason, it is not retrieving the next items in the category, it is only finding the next item in that tag grouping. The category is "rentals" (for example) and the tag is "neighborhood x" (for example). But the links are limited, only showing for "neighborhood x" (tag) and not for "rentals" (category).

Is there any way to make the navigation links use everything in the category instead of the tag?

share|improve this question


First, you don't have to put '&laquo; %link' in, as this is already the default value.

WPDB Query

Your "Problem" is, that the underlying funtion adjacent_post_link() uses get_adjacent_post() if it's not querying an attachment.

This then builds the following JOIN part for the query:

INNER JOIN $wpdb->term_relationships 
    AS tr 
    ON p.ID = tr.object_id 
INNER JOIN $wpdb->term_taxonomy tt 
    ON tr.term_taxonomy_id = tt.term_taxonomy_id

Filter the query, before executing it:

Here's the filter description from the core function:

apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories )

That means, that you can build the following plugin - IF you set the $in_same_cat argument to TRUE (Hint: This is the 3rd argument for next_/previous_post_link()).

! defined( 'ABSPATH' ) AND exit;
/** Plugin Name: Intercept Next/Prev Posts links query <code>JOIN</code>. */

# Alter »PREVIOUS« query
function wpse64909_prev_link_query( $join, $in_same_cat, $excluded_cats )
    global $post;

    // Example: Only for specific post types.
    // $post_type = $post->post_type
    // if ( ! in_array( $post_type, array( 'post', 'page' ) )
    //     return;

    // alter $join - you have $in_same_cat and $excluded_cats to assist you

    return $join;
add_filter( 'get_previous_post_join', 'wpse64909_nextprev_link_query', 10, 3 );

# Alter »NEXT« query
function wpse64909_next_link_query( $join, $in_same_cat, $excluded_cats )
    global $post;

    // alter $join - you have $in_same_cat and $excluded_cats to assist you

    return $join;
add_filter( 'get_next_post_join', 'wpse64909_nextprev_link_query', 10, 3 );
share|improve this answer
Thanks so much for your response, I really appreciate it. But when I implement and activate the plugin, and add the TRUE parameter to my links like so: PREV: <?php previous_post_link('%link', 'Prev post in category', TRUE); ?><br> NEXT: <?php next_post_link('%link', 'Next post in category', TRUE); ?> the following happens:<br> - it does seem to now stick to categories, BUT - it crosses more than one category, where I want it to stay in just the one it is in. – Stephen Sep 12 '12 at 12:13
Please A) look at the arguments of your function: You got a last one too, that will allow you to exclude categories. And B) The plugin does nothing so far. You'll have to alter the query to your needs, yourself. It's just a wrapper and you'll have to read the comments, do your var_dump and other debug and then alter the $join. I'm not writing "do this for me"-plugins in here (most of the time), as I'm not offering free work here, but assistance to get your task done. – kaiser Sep 12 '12 at 12:18
OK, thanks for your advice and help, I appreciate it. – Stephen Sep 12 '12 at 12:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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