[wp-trac] [WordPress Trac] #64390: get_adjacent_post() SQL changes can loop infinitely in Storefront/derivative theme code

WordPress Trac noreply at wordpress.org
Tue Dec 30 18:32:35 UTC 2025


#64390: get_adjacent_post() SQL changes can loop infinitely in
Storefront/derivative theme code
-----------------------------------------+---------------------------------
 Reporter:  jmdodd                       |       Owner:  peterwilsoncc
     Type:  defect (bug)                 |      Status:  reviewing
 Priority:  normal                       |   Milestone:  6.9.1
Component:  Posts, Post Types            |     Version:  6.9
 Severity:  normal                       |  Resolution:
 Keywords:  has-patch changes-requested  |     Focuses:  template,
                                         |  performance
-----------------------------------------+---------------------------------

Comment (by jmdodd):

 We were able to mitigate this at a platform level using the following code
 to capture looping behavior in derivative themes:

 {{{#!php
 <?php

 add_action( 'init', function() {
         if ( defined( 'WC_PLUGIN_FILE' ) ) {
                 add_filter( 'get_next_post_where',
 'wp69_product_adjacent_where', 99999, 5 );
                 add_filter( 'get_previous_post_where',
 'wp69_product_adjacent_where', 99999, 5 );
         }
 } );

 function wp69_product_adjacent_where( $where, $in_same_term,
 $excluded_terms, $taxonomy, $post ) {
         if ( ! ( $post instanceof WP_Post ) || ! property_exists( $post,
 'post_type' ) || 'product' !== $post->post_type ) {
                 return $where;
         }

         // If the same WHERE repeats too many times, force no results to
 be returned.
         $threshold = 5;
         static $where_key = null;
         static $where_count;

         $this_where_key = md5( $where );
         if ( $this_where_key !== $where_key ) {
                 $where_key = $this_where_key;
                 $where_count = 0;
         } else {
                 $where_count = $where_count + 1;
         }

         if ( $where_count >= $threshold ) {
                 return $where . ' AND 1=0';
         }
         return $where;
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/64390#comment:21>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list