Home » Blog » Order Posts by a Custom Key

Order Posts by a Custom Key

Just recently, we completed a website using WordPress for a realtor named Jon Dishon. One of his requests was to have his listing be ordered by price. Since we created his listings from simple posts with WordPresss’ Custom Keys, it was difficult for us to do this since Price is one of those keys. We wrote some special code to join the custom keys to the query being run for a specific page. We put the code in the functions.php page of the custom theme we created. Below is the code to sort by a custom key:

add_filter('get_previous_post_sort', 'sort_it');
add_filter('get_next_post_sort', 'sort_it');
add_filter('posts_orderby', 'sort_it' );
add_filter('posts_join', 'join_it' );
add_filter('posts_where', 'where_it' );

function sort_it () {
	global $wpdb;
	return " $wpdb->postmeta.meta_value DESC ";
}

function join_it( $join ) {
	global $wpdb;
	$join .= " LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id) ";
	return $join;
}

function where_it( $where ) {
	global $wpdb;
	$where .= "AND $wpdb->postmeta.meta_key = '' ";
	return $where;
}

NOTE: This code will run on every page whether you want it to or not. Be sure to use it only on the pages you want by wrapping this code in any of the conditional tags listed on the Codex.

How Can We Help You?

You can fill out the form below to reach out to us about your project. We promise not to resell your contact information or to send you spam (we hate it too).

3 comments

  1. sutaze says:

    great code!
    but, how can i use it just for my Archived posts? when i wrap the code in WP fnc <?php if (is_archive()) ... it is not functioning. Please can you help me with this? I need sort my posts by custom_value in category, tag and archive [(is_category) (is_tag) and (is_archive)] pages. thank you in advance

  2. Brandon says:

    @sutaze Huh. I never thought of using it in an archive. Seems like archives are meant to be grouped by date, and therefore sorting by a custom key wouldn’t be appropriate. Maybe what would work is to reevaluate what archives mean in your instance. For example, maybe you could add an additional custom key to a post to identify it as an archive. If this was the case, you’d just have to add a check for that custom key in your archives page code. Can you put up a link to the site you’re working on so we can take a look at what you’re trying to do?

  3. sutaze says:

    hi, i wil try to explain it better:
    on my site http://www.sutaze24.sk are prize competitions over the internet. They are something like events, every competition ends in another day and I need sort my posts by custom key in categories pages (e.g. http://www.sutaze24.sk/auto-moto), tags pages (e.g. http://www.sutaze24.sk/ceny/dovolenka).
    Now when you take a look in category e.g. /auto-moto , you will see at first place a conntest, which has ended (greyed links mean an ended contest).

    The whole thing is, how to use your code just in some places, because on index i need the default sorting.

Leave a Reply

Your email address will not be published. Required fields are marked *