1

I am creating a list of all users in wordpress. For that I am using WP_User_Query with meta_query so that I can show only filtered users. I am stuck where I want to add a custom query filters in 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

and a custom JOIN

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 a custom WHERE condition

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)))))

and custom having clause

having distance < $_REQUEST['geo-radius']

I know that how we add query filters for POSTS but I am not able to add query filters for USERS

Please guide me or any reference url.

Thanks

  • Mayby try with custom field parameters? codex.wordpress.org/Class_Reference/… – Paul Sep 30 '15 at 13:44
  • Through custom field parameters you can use custom fields that you created through code and exist in DB. But I am looking to add new field in "select", add "join" and add having clause dynamically before query run same as we can do for POSTS query. Any idea? – avinashphp Oct 8 '15 at 7:28
  • There is no way to add custom filter in WP User Query according to my question. So finally I write normal SQL and use custom paging function and display data through for loop. Thanks for everyone. – avinashphp Oct 19 '15 at 10:31
4

The pre_user_query action hook in WordPress will allow you alter the SQL statements in the WP_User_Query object before they are run against the database. Note, this is an action, not a filter, so there's no need to return the $user_query that gets passed in.

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

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.