Previous/next link to post in category, NOT tag?

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

<?php previous_post_link('&laquo; %link') ?> | 
<?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).

Read More

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

Related posts

Leave a Reply

1 comment

  1. Defaults

    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()).

    <?php
    ! 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 );