Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

joh3 on "Sort Posts by Number / Convert longtext to int"

$
0
0

Hello,
I want to list my custom Posts by their ranking.
The Problem is, that the meta_value is saved as a longtext in the database.
So the list shows up the wrong order:
1
10
2
3
4

Here is my code of getting the data fomr the database:

add_filter('posts_orderby', 'ranking_order');

function ranking_order($orderby)
{
		global $wpdb,$wp_query;

		$orderby = " (SELECT distinct $wpdb->postmeta.meta_value
		from $wpdb->postmeta
		where ($wpdb->posts.ID = $wpdb->postmeta.post_id) AND $wpdb->postmeta.meta_key ='_ranking'
		ORDER BY CAST('_ranking' AS DECIMAL)DESC)
		ASC, $wpdb->posts.post_date DESC";
	return $orderby;
}

I'd be glad for any help.


Viewing all articles
Browse latest Browse all 8245

Trending Articles