Reputation points as numeric values

4.54K viewsIssues
0

Hi There,

I’m using reputation points from Anspress along with Userpro plugin.  When I list users by reputation points in Userpro, everything works as expected.  However, when I want to order the list by rep points descending, points are consider as strings and not numbers.  For example those points:

23, 43, 123

will be ordered like this:

123, 23, 43

when it should show:

123, 43, 23

the reason being that first character is considered for the sorting….

Any thoughts on how I can easily convert Reputation points to numeric values??

Thanks

0
cast(meta.value as unsigned)

Use this in your MySql query. Here is an example:

https://github.com/anspress/anspress/blob/master/includes/class-user.php#L324

Thanks Rahul, however it doesn’t seem to work. I replaced this in my Anspress user-class.php file:

===============================
public function user_sort_by_reputation($query)
{
if (isset($query->query_vars[‘ap_query’]) && $query->query_vars[‘ap_query’] == ‘user_sort_by_reputation’) {
global $wpdb;
$query->query_orderby = ‘ORDER BY cast(mt1.meta_value AS DECIMAL) DESC’;
}

return $query;
}
================================

by this as per your recommendations:

==========================
public function user_sort_by_reputation($query) {
global $wpdb;
if ( isset( $query->query_vars[‘ap_query’] ) ) {
$query->query_where = $query->query_where.” AND (apm1.user_id IS NULL OR ( apm1.meta_value != 1) )”;
$query->query_from = $query->query_from. ” LEFT JOIN {$wpdb->usermeta} AS apm1 ON ( {$wpdb->users}.ID = apm1.user_id AND apm1.meta_key = ‘hide_profile’ )”;
if ( $query->query_vars[‘ap_query’] == ‘user_sort_by_reputation’ ) {
$query->query_orderby = ‘ORDER BY cast(mt1.meta_value AS DECIMAL) DESC’;
}
}
return $query;
}
============================

Why are you replacing AnsPress query..? you should replace in your query. Update your question with your query code.

You are viewing 1 out of 1 answers, click here to view all answers.