Advertisement
helgatheviki

Prev next posts by menu order

Mar 20th, 2024 (edited)
739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.85 KB | None | 0 0
  1.  
  2. /* REORDER POST NAV */
  3. add_filter( 'get_next_post_where', 'lbardugo_adjacent_post_where', 10, 5 );
  4. add_filter( 'get_previous_post_where', 'lbardugo_adjacent_post_where', 10, 5 );
  5. /**
  6.  * Overrides the post navigation to sort by menu order.
  7.  *
  8.  * @see https://wordpress.stackexchange.com/a/73194/6477
  9.  *
  10.  * @since 1.0.0
  11.  *
  12.  * @param string       $where          The `WHERE` clause in the SQL.
  13.  * @param bool         $in_same_term   Whether post should be in the same taxonomy term.
  14.  * @param int[]|string $excluded_terms Array of excluded term IDs. Empty string if none were provided.
  15.  * @param string       $taxonomy       Taxonomy. Used to identify the term used when `$in_same_term` is true.
  16.  * @param WP_Post      $post           WP_Post object.
  17.  * @return string
  18.  */
  19. function lbardugo_adjacent_post_where($where, $in_same_term, $excluded_terms, $taxonomy, $post ) {
  20.  
  21.   // Get out now if this isn't the query we want to filter
  22.     if ( !is_main_query() || !is_singular() ) {
  23.         return $where;
  24.     }
  25.    
  26.     // Check if filtering the next or previous clause.
  27.     // By default, sql is WHERE p.post_date > $date for next and < for previous.
  28.     // To sort by menu order ASC then we need to switch this.
  29.     if (strpos(current_filter(), 'next') !== false) {
  30.         $where = str_replace(">", "<", $where);
  31.     } else {
  32.         $where = str_replace("<", ">", $where);
  33.     }
  34.    
  35.     $patterns = array();
  36.     $patterns[] = '/post_date/';
  37.     $patterns[] = '/\'[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\'/';
  38.     $replacements = array();
  39.     $replacements[] = 'menu_order';
  40.     $replacements[] = $post->menu_order;
  41.     return preg_replace( $patterns, $replacements, $where );
  42.  
  43. }
  44.  
  45. add_filter( 'get_next_post_sort', 'lbardugo_adjacent_post_sort', 10, 3 );
  46. add_filter( 'get_previous_post_sort', 'lbardugo_adjacent_post_sort', 10, 3 );
  47. /**
  48.  * Overrides the post navigation to sort by menu order.
  49.  *
  50.  * @see https://github.com/WordPress/wordpress-develop/blob/6.4/src/wp-includes/link-template.php#L1988
  51.  *
  52.  * @since 1.0.0
  53.  *
  54.  * @param string $order_by The `ORDER BY` clause in the SQL.
  55.  * @param WP_Post $post    WP_Post object.
  56.  * @param string  $order   Sort order. 'DESC' for previous post, 'ASC' for next.
  57.  * @return string
  58.  */
  59. function lbardugo_adjacent_post_sort($order_by, $post, $order) {
  60.  
  61.   // Get out now if this isn't the query we want to filter
  62.     if ( !is_main_query() || !is_singular() ) {
  63.         return $order_by;
  64.     }
  65.    
  66.     // Check if filtering the next or previous clause.
  67.     if (strpos(current_filter(), 'next') !== false) {
  68.         $order_by = str_replace("ASC", "DESC", $order_by);
  69.     } else {
  70.         $order_by = str_replace("DESC", "ASC", $order_by);
  71.     }
  72.    
  73.     $pattern = '/post_date/';
  74.     $replacement = 'menu_order';
  75.     return preg_replace( $pattern, $replacement, $order_by );
  76.  
  77. }
Tags: wordpress
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement