ทำความเข้าใจเกี่ยวกับการฝึกงาน
คำสั่ง "เรียงลำดับ" ของโพสต์ถัดไป (ก่อนหน้า / ถัดไป) ไม่ใช่การเรียงลำดับ "เรียง" จริงๆ มันเป็นแบบสอบถามที่แยกต่างหากในแต่ละคำขอ / หน้าแต่มันเรียงลำดับแบบสอบถามโดยpost_date- หรือผู้ปกครองโพสต์ถ้าคุณมีการโพสต์ลำดับชั้นเป็นวัตถุที่ปรากฏในปัจจุบัน
เมื่อคุณดูที่ internals ของ next_post_link()แล้วคุณจะเห็นว่ามันเป็นพื้นเสื้อคลุม API adjacent_post_link()สำหรับ ฟังก์ชั่นในภายหลังเรียกget_adjacent_post()ภายในด้วยการ$previousตั้งค่าอาร์กิวเมนต์ / ธงbool(true|false)เพื่อคว้าลิงค์โพสต์ถัดไปหรือก่อนหน้า
สิ่งที่จะกรอง?
หลังจากขุดลึกลงไปแล้วคุณจะเห็นว่าget_adjacent_post() ลิงค์แหล่งที่มามีตัวกรองที่ดีสำหรับผลลัพธ์ (หรือผลการสืบค้น): (ชื่อตัวกรอง / อาร์กิวเมนต์)
- "get_{$adjacent}_post_join"
 - $join
// Only if `$in_same_cat`
// or: ! empty( $excluded_categories` 
// and then: 
// " 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"; 
// and if $in_same_cat then it APPENDS: 
// " AND tt.taxonomy = 'category' 
// AND tt.term_id IN (" . implode(',', $cat_array) . ")";
$in_same_cat
$excluded_categories
 
- "get_{$adjacent}_post_where"
 - $wpdb->prepare(
      // $op = $previous ? '<' : '>'; | $current_post_date
       "WHERE p.post_date $op %s "
      // $post->post_type
      ."AND p.post_type = %s "
      // $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' 
      // AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')'; 
      // OR empty string if $in_same_cat || ! empty( $excluded_categories
      ."AND p.post_status = 'publish' $posts_in_ex_cats_sql "
    ",
    $current_post_date,
    $post->post_type
)
$in_same_cat
$excluded_categories
 
- "get_{$adjacent}_post_sort"
 - "ORDER BY p.post_date $order LIMIT 1"`
 
ดังนั้นคุณสามารถทำได้ มากกับมัน ที่เริ่มต้นด้วยการกรองWHEREคำสั่งรวมถึงJOINตาราง ed และORDER BYคำสั่ง
ผลลัพธ์จะถูกแคชในหน่วยความจำสำหรับคำขอปัจจุบันดังนั้นจึงไม่เพิ่มการสืบค้นเพิ่มเติมถ้าคุณเรียกใช้ฟังก์ชันนั้นหลายครั้งในหน้าเดียว
การสร้างแบบสอบถามอัตโนมัติ
ในฐานะที่เป็น@StephenHarrisชี้ให้เห็นในความคิดเห็นที่มีฟังก์ชั่นหลักที่อาจเข้ามามีประโยชน์เมื่อมีการสร้างแบบสอบถาม SQL: get_meta_sql()- ตัวอย่างใน Codex โดยทั่วไปฟังก์ชั่นนี้ใช้เพื่อสร้างคำสั่ง meta SQL ที่นำมาใช้WP_Queryแต่คุณสามารถใช้มันในกรณีนี้ (หรืออื่น ๆ ) เช่นกัน WP_Queryอาร์กิวเมนต์ที่คุณโยนลงไปในมันเป็นอาร์เรย์เดียวกันแน่นอนว่าจะเพิ่มไปยัง
$meta_sql = get_meta_sql(
    $meta_query,
    'post',
    $wpdb->posts,
    'ID'
);
ค่าส่งคืนคืออาร์เรย์:
$sql => (array) 'join' => array(),
        (array) 'where' => array()
ดังนั้นคุณสามารถใช้$sql['join']และ$sql['where']ในการโทรกลับของคุณ
พึ่งจำไว้
ในกรณีของคุณสิ่งที่ง่ายที่สุดคือการดักมันในปลั๊กอิน (mu) ขนาดเล็กหรือในไฟล์ themes.php ของคุณและเปลี่ยนฟังก์ชั่นขึ้นอยู่กับ$adjacent = $previous ? 'previous' : 'next';ตัวแปรและ$order = $previous ? 'DESC' : 'ASC';ตัวแปร:
ชื่อตัวกรองที่แท้จริง
ดังนั้นชื่อตัวกรองคือ:
- get_previous_post_join,- get_next_post_join
- get_previous_post_where,- get_next_post_where
- get_previous_post_sort,- get_next_post_sort
ล้อมรอบเป็นปลั๊กอิน
... และการเรียกกลับตัวกรอง (เช่น) จะเป็นดังนี้:
<?php
/** Plugin Name: (#73190) Alter adjacent post link sort order */
function wpse73190_adjacent_post_sort( $orderby )
{
    return "ORDER BY p.menu_order DESC LIMIT 1";
}
add_filter( 'get_previous_post_sort', 'wpse73190_adjacent_post_sort' );
add_filter( 'get_next_post_sort', 'wpse73190_adjacent_post_sort' );