1
votes

I am working on a custom plugin in wordpress. I have a weird issue with one sql.

SQL:

SELECT SQL_CALC_FOUND_ROWS wp_posts. *
FROM wp_posts
INNER JOIN wp_term_relationships 
ON ( wp_posts.ID = wp_term_relationships.object_id )
INNER JOIN wp_term_taxonomy 
ON ( wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id )
WHERE 1 =1
AND wp_term_taxonomy.taxonomy = 'category'
AND wp_term_taxonomy.term_id IN ('23')
AND (
wp_posts.post_author =1
)
AND wp_posts.post_type = 'post'
AND (
wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'private'
)
GROUP BY wp_posts.ID
ORDER BY `wp_posts`.`as_stats_rating` DESC
LIMIT 0 , 30 

Its returning the correct data but its not sorting results according to as_stats_rating. I am stumped. Does anyone know what I am doing wrong?

Edit 1 : Update

Here is the structure of wp_posts: alt text


Sample result:

ID  as_stats_rating
1221   8
1222   10

All fields in sample results are :
ID post_author post_date post_date_gmt post_content post_title post_excerpt post_status comment_status ping_status post_password post_name to_ping pinged post_modified post_modified_gmt post_content_filtered post_parent guid menu_order post_type post_mime_type comment_count as_stats_numviews as_stats_numvotes as_stats_votestotal as_stats_rating


By the way, its not only about 'order by wp_posts.as_stats_rating', 'order by wp_posts.as_stats_numviews' have the same issue. (just to clear up, if you are wondering about as_stats_rating having varchar type)

1
i would suspect a conflict with the limit statement. as a check, take the limit off and see if the order by is correct.Randy
@Randy : No. Just checked by removing limit clause, the issue persist.Krishna Kant Sharma
It already has a group by, what should be the group by?Krishna Kant Sharma
Is wp_post.ID an auto_increment?methodin
What does a sample result set look like?methodin

1 Answers

1
votes

Any chance that the as_stats_rating field is a string (char/text) datatype and not a numeric (float/int/decimal) datatype?