I have a plugin that will sorts posts on the search page and taxonomy page. The loop has already taken care of narrowing down the results, so what i need is to be able to sort the post my a custom meta value. In this case price. I want to be able to reach into the existing query, replace any special characters, then sort based on the clean meta value.
I used this select, but the problem is it querys all posts on the database instead of the reults found in wp_query and this can slow down the sort tremendously
function join_it( $join ) {
global $wpdb;
$sym = get_option('sort_by_ignore');
$join .= "LEFT JOIN (SELECT $wpdb->prefix" . "postmeta.post_id AS ID, CAST(REPLACE(REPLACE(REPLACE($wpdb->prefix" . "postmeta.meta_value, ',', ''), '" . $sym . "',''),' ','') AS SIGNED) AS price FROM $wpdb->prefix" . "postmeta WHERE meta_key = 'my_price'" . ") P ON (P.ID = wp_posts.ID)";
return $join;
}
add_filter('posts_join', 'join_it' );
$args = array_merge( $wp_query->query, array('caller_get_posts' => 1,'paged' => $paged ));
query_posts($args);