Make WordPress Core

Opened 10 years ago

Last modified 5 weeks ago

#40660 new defect (bug)

the_post_navigation not excluding terms - wp 4.7.4

Reported by: Sbaxter_Quba Owned by:
Priority: normal Milestone: Awaiting Review
Component: Query Version: 4.7.4
Severity: normal Keywords: close
Cc: Focuses:

Description

I'm retrieving the post navigation as so

<?php
$args = array(
        'in_same_term' => true,
        'excluded_terms'     => '129',
        'taxonomy' => 'category'
    );
    the_post_navigation( $args )

I've debugged and it constructs the $where clause here as it should (properly excluding the term) ​https://github.com/WordPress/WordPress/blob/master/wp-includes/link-template.php#L1647

$where =
AND tt.taxonomy = 'category' AND tt.term_id IN (127) AND p.ID NOT IN ( SELECT tr.object_id FROM wp_term_relationships tr LEFT JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.term_id IN (129) ) AND ( p.post_status = 'publish' OR p.post_status = 'private' )

This is then replaced here ​https://github.com/WordPress/WordPress/blob/master/wp-includes/link-template.php#L1716 it improperly constructs the where clause so that the result is as follows

$where =
WHERE p.menu_order > '2' AND p.post_type = 'post' AND p.post_status = 'publish'

This means i cannot excluded other categories from the post navigation or specify which categories to exclude.

Change History (3)

This ticket was mentioned in ​Slack in #core by noisysocks. ​View the logs.


6 years ago

#2 @noisysocks
6 years ago

  • Component General → Query
  • Keywords needs-testing added

#3 @huzaifaalmesbah
5 weeks ago

  • Keywords close added; needs-testing removed

Reproduction Report

I investigated and tested this ticket on WordPress Trunk (7.2-alpha-63166-src) to verify whether excluded_terms properly excludes terms in adjacent post queries.

Environment

  • WordPress: 7.2-alpha-63166-src
  • PHP: 8.3.35
  • Web Server: nginx/1.31.6
  • Database: MySQL 9.7 / mysqli 8.3

Test Scenario

  1. Created two categories: Normal Category and Excluded Category.
  2. Created three posts:
    • Post 1: Normal Category (Date: 2026-01-01)
    • Post 2: Excluded Category (Date: 2026-01-02)
    • Post 3: Normal Category (Date: 2026-01-03)
  3. Viewed Post 3 and retrieved adjacent posts using both get_adjacent_post() and the_post_navigation():
    // Exclude the term
    $prev = get_adjacent_post( false, (string) $excluded_cat_id, true, 'category' );
    the_post_navigation( array( 'excluded_terms' => $excluded_cat_id ) );
    

Results

  • get_adjacent_post() without exclusions correctly returned Post 2.
  • get_adjacent_post() with excluded_terms correctly skipped Post 2 and returned Post 1.
  • the_post_navigation() rendered navigation pointing to Post 1, successfully excluding Post 2.

Analysis

In the ticket description, the reported $where clause was:
WHERE p.menu_order > '2' AND p.post_type = 'post' AND p.post_status = 'publish'

WordPress Core's get_adjacent_post() operates strictly on p.post_date and does not use p.menu_order in adjacent post queries. The query mentioned in the report indicates that a custom theme or third-party plugin hooked into the get_{$adjacent}_post_where filter and completely overwrote $where, stripping Core's category exclusion query.

Conclusion & Recommendation

On a stock WordPress installation, the_post_navigation() and get_adjacent_post() correctly exclude the specified terms. Since Core behaves as expected and the issue was caused by an external filter overriding the query, I recommend closing this ticket as invalid or worksforme.

Removing needs-testing and adding close for maintainer review.

Note: See TracTickets for help on using tickets.