wordpress | April 8th, 2011

If you want to make postsĀ orderby custom field … use below code .

proirity = Custom Field name

global $wp_query;

function priority_select($sql){

global $wpdb;

if (strpos($sql, ‘proirity’) !== false){

$sql = str_replace(“ORDER BY {$wpdb->postmeta}.meta_key”, “ORDER BY CAST({$wpdb->postmeta}.meta_key AS UNSIGNED)”, $sql);


return $sql;


add_filter(‘query’, ‘priority_select’);

query_posts( array_merge(array(‘meta_key’ => ‘proirity’, ‘orderby’=> ‘meta_value’, ‘order’ => ‘asc’), $wp_query->query));

// loop code


There are no comments yet, add one below.

Leave a Comment

You must be logged in to post a comment.