钩子文档

posts_distinct

💡 云策文档标注

概述

posts_distinct 是一个 WordPress 过滤器,用于修改查询中的 DISTINCT 子句,控制返回结果是否去重。它允许开发者根据条件自定义 DISTINCT 行为,例如在特定情况下允许重复。

关键要点

  • 过滤器名称:posts_distinct,用于过滤查询的 DISTINCT 子句。
  • 参数:$distinct(DISTINCT 子句字符串)和 $query(WP_Query 实例,通过引用传递)。
  • 返回 "DISTINCTROW" 与 "DISTINCT" 效果相同,均用于去重。
  • 在 WP_Query::get_posts() 中使用,影响基于查询变量检索的帖子数组。
  • 自 WordPress 2.1.0 版本引入。

代码示例

add_filter('posts_distinct', 'search_distinct');

function search_distinct() {
    if ( $this->allow_duplicates )
        return ""; // filter has no effect
    return "DISTINCT";
}

注意事项

示例代码来自用户贡献,需注意 $this->allow_duplicates 变量可能未定义,实际使用时需确保上下文正确或调整条件逻辑。


📄 原文内容

Filters the DISTINCT clause of the query.

Parameters

$distinctstring
The DISTINCT clause of the query.
$queryWP_Query
The WP_Query instance (passed by reference).

More Information

Returning “DISTINCTROW” from the filter has the same effect on the query as “DISTINCT”.

Source

$distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );

Changelog

Version Description
2.1.0 Introduced.

User Contributed Notes