如何在WP_USER_QUERY中添加自定义查询过滤器

时间:2015-09-30 作者:avinashphp

我正在wordpress中创建所有用户的列表。为此,我将WP\\u User\\u Query与meta\\u Query结合使用,以便只显示筛选过的用户。我想在SELECT中添加自定义查询过滤器,但遇到了问题

p.distance_unit
             * DEGREES(ACOS(COS(RADIANS(p.latpoint))
             * COS(RADIANS(mt30.meta_value ))
             * COS(RADIANS(p.longpoint) - RADIANS(mt31.meta_value))
             + SIN(RADIANS(p.latpoint))
             * SIN(RADIANS(mt30.meta_value)))) AS distance
和自定义联接

SELECT  \'.$_REQUEST[\'user_lat\'].\'  AS latpoint,  \'.$_REQUEST[\'user_long\'].\' AS longpoint,
            \'.$_REQUEST[\'geo-radius\'].\'.0 AS radius,      111.045 AS distance_unit
) AS p ON 1=1
还有一个习惯

AND (mt30.meta_value
 BETWEEN p.latpoint  - (p.radius / p.distance_unit)
     AND p.latpoint  + (p.radius / p.distance_unit)
AND mt31.meta_value
 BETWEEN p.longpoint - (p.radius / (p.distance_unit * COS(RADIANS(p.latpoint))))
     AND p.longpoint + (p.radius / (p.distance_unit * COS(RADIANS(p.latpoint)))))
和海关拥有条款

having distance < $_REQUEST[\'geo-radius\']
我知道如何为帖子添加查询过滤器,但我无法为用户添加查询过滤器

请指导我或任何参考url。

谢谢

1 个回复
SO网友:Pete Nelson

这个pre_user_query WordPress中的操作挂钩允许您在对数据库运行WP\\u User\\u查询对象中的SQL语句之前更改它们。注意,这是一个操作,不是筛选器,因此不需要返回$user_query 那会被传进来。

add_action( \'pre_user_query\', \'add_my_custom_queries\' );

function add_my_custom_queries( $user_query ) {

   $user_query->query_fields .= \', my_custom_field \';  // additional fields 
   $user_query->query_from .= \' INNER JOIN my_table \'; // additional joins here
   $user_query->query_where .= \' AND field=\'value\' \'; // additional where clauses
   $user_query->query_orderby .= \' ORDER BY my_custom_field \'; // additional sorting
   $user_query->query_limit .= \'\'; // if you need to adjust paging

}

相关推荐

Get users that likes the post

我有一个类似ajax的帖子系统,当用户喜欢帖子时,系统会在帖子中添加一个帖子元,并将其命名为(post\\u liked\\u users)。post\\u likes\\u users元值示例:a:1:{s:6:\"user-1\";i:1;}现在,如何才能获得喜欢该帖子的用户?注意:我不能像杰克·约翰逊那样只使用foreach答案,我必须使用WP\\u User\\u Query或WP\\u Query,因为我要使用offest和number。