钩子文档

get_{$adjacent}_post_sort

💡 云策文档标注

概述

此过滤器用于修改相邻文章查询中的 SQL ORDER BY 子句,允许开发者自定义排序逻辑。它基于文章类型(next 或 previous)动态生成钩子名称,如 get_next_post_sort 或 get_previous_post_sort。

关键要点

  • 过滤器名称动态部分 $adjacent 表示相邻类型,可为 'next' 或 'previous'。
  • 参数包括 $order_by(SQL ORDER BY 子句)、$post(WP_Post 对象)和 $order(排序顺序,'DESC' 用于上一篇文章,'ASC' 用于下一篇文章)。
  • 默认排序基于 post_date 和 ID,确保日期相同时的确定性排序。
  • 相关函数为 get_adjacent_post(),用于检索相邻文章。

代码示例

$sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order, p.ID $order LIMIT 1", $post, $order );

注意事项

  • 版本 6.9.0 添加了 ID 排序以处理相同日期的文章。
  • 版本 4.9.0 引入了 $order 参数。
  • 版本 4.4.0 引入了 $post 参数。
  • 此过滤器自 2.5.0 版本引入。

📄 原文内容

Filters the ORDER BY clause in the SQL for an adjacent post query.

Description

The dynamic portion of the hook name, $adjacent, refers to the type of adjacency, ‘next’ or ‘previous’.

Possible hook names include:

  • get_next_post_sort
  • get_previous_post_sort

Parameters

$order_bystring
The ORDER BY clause in the SQL.
$postWP_Post
WP_Post object.
$orderstring
Sort order. 'DESC' for previous post, 'ASC' for next.

Source

$sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order, p.ID $order LIMIT 1", $post, $order );

Changelog

Version Description
6.9.0 Adds ID sort to ensure deterministic ordering for posts with identical dates.
4.9.0 Added the $order parameter.
4.4.0 Added the $post parameter.
2.5.0 Introduced.