Title: Adding a new orderby= function
Last modified: August 19, 2016

---

# Adding a new orderby= function

 *  [sonny_b](https://wordpress.org/support/users/sonny_b/)
 * (@sonny_b)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/adding-a-new-orderby-function/)
 * Hi.
    How would one go about adding a new option to ‘orderby=’ when querying posts?
   I have a custom field named ‘crew_order’ (where the meta_value is a single number),
   and whilst I’ve found various scraps of code to call in the MySQL data (such 
   as here [http://wordpress.org/support/topic/121011?replies=15](http://wordpress.org/support/topic/121011?replies=15)),
   it’s not quite what I’m looking for, as I need to keep it as part of a custom
   query_string.
 * How, and where, would I go about adding “orderby=crew_order” as a function?
 * Many thanks

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/adding-a-new-orderby-function/#post-907511)
 * Might look at [Displaying_Posts_Using_a_Custom_Select_Query](http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query)
   and adjust the code accordingly.
 * Also, Otto has info for adding an orderby value to query_posts in this thread:
   
   [http://wordpress.org/support/topic/157515?replies=5#post-699291](http://wordpress.org/support/topic/157515?replies=5#post-699291)
 *  Thread Starter [sonny_b](https://wordpress.org/support/users/sonny_b/)
 * (@sonny_b)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/adding-a-new-orderby-function/#post-907715)
 * Thanks, I’ll check those out.
 *  Thread Starter [sonny_b](https://wordpress.org/support/users/sonny_b/)
 * (@sonny_b)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/adding-a-new-orderby-function/#post-907723)
 * Fantastic, I got it working with the following code:
 *     ```
       <ul class="crewlist">
       				<?php
       					 $querystr = "
       						SELECT * FROM $wpdb->posts
       						LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
       						LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
       						LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
       						WHERE $wpdb->term_taxonomy.term_id = 4
       						AND $wpdb->term_taxonomy.taxonomy = 'category'
       						AND $wpdb->posts.post_status = 'publish'
       						AND $wpdb->postmeta.meta_key = 'crew_order'
       						ORDER BY $wpdb->postmeta.meta_value ASC
       						LIMIT 9
       					";
       					  $crew_order = $wpdb->get_results($querystr, OBJECT);
       					?>
       <?php if ($crew_order): ?>
         <?php foreach ($crew_order as $post): ?>
           <?php setup_postdata($post); ?>
       				<li><?php if ($currentId!==$post->ID) { ?><a href="<?php the_permalink();?>"><?php the_title(); ?></a><?php } else { ?><?php the_title(); ?><?php } ?></li>
         <?php endforeach; ?>
         <?php endif; ?>
       				</ul>
       ```
   
 * Is there any way to get it to auto paginate once the LIMIT has been reached?
 * Thanks so much for the help.
 *  Thread Starter [sonny_b](https://wordpress.org/support/users/sonny_b/)
 * (@sonny_b)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/adding-a-new-orderby-function/#post-907724)
 * Regarding pagination –
    I found some great help from Aesqe here:
 * [http://wordpress.org/support/topic/154300?replies=7](http://wordpress.org/support/topic/154300?replies=7)
 * But I can’t get it working with the Paginav plugin. Anyone have any thoughts?

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Adding a new orderby= function’ is closed to new replies.

## Tags

 * [custom sort order](https://wordpress.org/support/topic-tag/custom-sort-order/)
 * [order by](https://wordpress.org/support/topic-tag/order-by/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [sonny_b](https://wordpress.org/support/users/sonny_b/)
 * Last activity: [17 years, 5 months ago](https://wordpress.org/support/topic/adding-a-new-orderby-function/#post-907724)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
