Title: Sort by custom value fields?
Last modified: August 18, 2016

---

# Sort by custom value fields?

 *  [Xander](https://wordpress.org/support/users/xander/)
 * (@xander)
 * [20 years, 8 months ago](https://wordpress.org/support/topic/sort-by-custom-value-fields/)
 * I wonder if the query_posts() function has the ability to orderby a custom value
   field?

Viewing 15 replies - 1 through 15 (of 17 total)

1 [2](https://wordpress.org/support/topic/sort-by-custom-value-fields/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/sort-by-custom-value-fields/page/2/?output_format=md)

 *  [marksbrain](https://wordpress.org/support/users/marksbrain/)
 * (@marksbrain)
 * [20 years, 7 months ago](https://wordpress.org/support/topic/sort-by-custom-value-fields/#post-264874)
 * I wonder that as well. My hunch is that the answer is no, because if it were 
   possible everyone would be doing it and there would be some record of it on the
   Internet. (I’ve searched!) I think it doesn’t work because custom fields are 
   metadata or something like that.
 * Anyone smarter than Xander and I care to weigh in?
 *  [marksbrain](https://wordpress.org/support/users/marksbrain/)
 * (@marksbrain)
 * [20 years, 7 months ago](https://wordpress.org/support/topic/sort-by-custom-value-fields/#post-264878)
 * Bueller?
 *  [chujiu](https://wordpress.org/support/users/chujiu/)
 * (@chujiu)
 * [20 years, 7 months ago](https://wordpress.org/support/topic/sort-by-custom-value-fields/#post-264879)
 * hehe
 *  [marksbrain](https://wordpress.org/support/users/marksbrain/)
 * (@marksbrain)
 * [20 years, 6 months ago](https://wordpress.org/support/topic/sort-by-custom-value-fields/#post-264892)
 * Okay, I’ve done some further research and I’m thinking there must be a way to
   get this to work using some combination of this type of code…
 *  {
    $q[‘orderby’] = ‘????’; $q[‘order’] = ‘ASC’; }
 * (which I found [here](http://jehiah.com/archive/wordpress-category-page-hacks).)
 * …and the [ Get Custom Field Values Plugin](http://www.coffee2code.com/archives/2004/06/30/plugin-get-custom/)
 * But how it would actually work is beyond me.
 * Anyone who knows more than me care to give a brother a hand?
 *  [jehiah](https://wordpress.org/support/users/jehiah/)
 * (@jehiah)
 * [20 years, 6 months ago](https://wordpress.org/support/topic/sort-by-custom-value-fields/#post-264893)
 * I should update my blog post (referenced above). The solution I use for wordpress
   1.5 is just a simple addition/edit to my .htaccess page
 * Note the orderby=title in the following line.
 * `RewriteRule ^archive/category/?(.*) /index.php?category_name=$1&orderby=title&
   order=asc&posts_per_page=500 [QSA]`
 * Turns out like [this](http://jehiah.com/archive/category/javascript/)
 *  [marksbrain](https://wordpress.org/support/users/marksbrain/)
 * (@marksbrain)
 * [20 years, 6 months ago](https://wordpress.org/support/topic/sort-by-custom-value-fields/#post-264894)
 * Very groovy.
 * So, is there any way to change the “orderby=title” to “orderby=customfield,” 
   or something like that?
 * (I’m dying for a little community support; I’ve been trying things on my own 
   and getting nowhere…Please help a brother out!)
 *  [marksbrain](https://wordpress.org/support/users/marksbrain/)
 * (@marksbrain)
 * [20 years, 6 months ago](https://wordpress.org/support/topic/sort-by-custom-value-fields/#post-264895)
 * Okay, so I think you wouldn’t really need to use the Get Custom Field Values 
   Plugin, because it seems there are now lots of ways to get at custom fields without
   using a plugin, as described [here](http://codex.wordpress.org/Using_Custom_Fields).
 * In fact, that page tells us something very interesting:
 * > The PostMeta information is stored in a new table, $wpdb->postmeta. This table
   > has four fields:
   >  meta_id: A unique id for each entry
   >  post_id: The ID of the post for this 
   > metadata meta_key: The name of the ‘key’ meta_value: The value associated with
   > the key
   > The values from this table are pulled into a structured multi-dimensional array
   > called $post_meta_cache, just after the $posts array is fetched in wp-blog-
   > header.php. This variable will only contain values for the list of posts fetched
   > for the current page build.
 * Cool. So, since the information is already there, it seems there would have to
   be a way to order by the meta_values assocated with a given meta_key. Right?
 * But what is it?
 * I think a lot of people would really dig it if we could come up with a way to
   do this. It would allow you to sort things in all kinds of useful ways.
 * In a site with reviews about music, you could have a meta_key:genre and sort 
   by the meta_values:rock, hip hop, folk, blues, etc.
 * In a site about future or past events, you could have meta_keys: Year, month,
   and day; and sort by the meta_values assigned to them.
 * Figuring this out would do a lot to make WordPress into even more of a mult-functional
   CMS. Custom fields have got to be more useful than just telling the world your
   mood. Anyone care to help out?
 *  [marksbrain](https://wordpress.org/support/users/marksbrain/)
 * (@marksbrain)
 * [20 years, 6 months ago](https://wordpress.org/support/topic/sort-by-custom-value-fields/#post-264896)
 * In the [discussion section of the codex page mentioned above](http://codex.wordpress.org/Talk:Using_Custom_Fields),
   there’s this interesting tidbit:
 * > If you just want to pull the custom field value
   >  out and use it however you
   > want, try this in your template (within The Loop):
   > <?php
   >  foreach(get_post_custom_values(‘music’) as $song) { echo $song; } ?>
   > Make sure the argument (i.e. custom field key) passed
   >  to get_post_custom_values()
   > is the correct one.
 * If you didn’t want to echo it (because you don’t want to see it, necessarily,
   just have it to order by) would it be possible to cut that part and run the rest
   of that function just as a way of making the value associated with that custom
   field key available for the orderby function? Would doing that allow you to set
   something to orderby=meta_value?
 * Does any of this make any sense? Or am I going crazy?
 *  Thread Starter [Xander](https://wordpress.org/support/users/xander/)
 * (@xander)
 * [20 years, 5 months ago](https://wordpress.org/support/topic/sort-by-custom-value-fields/#post-264899)
 * I never did find the way to make this happen… but I did accomplish what I wanted
   to do by sorting in the usual way with get posts and then throwing the array 
   through some if statements. Not elegant, but it worked for what I wanted to do(
   make a review database where a user can click on “top rated” or “before 2002”
   or whatever to see just a subset of all reviews)…
 *  [lellie](https://wordpress.org/support/users/lellie/)
 * (@lellie)
 * [20 years, 4 months ago](https://wordpress.org/support/topic/sort-by-custom-value-fields/#post-264909)
 * I’d love to know how this works too. 🙁 no one seems to know.
 *  Thread Starter [Xander](https://wordpress.org/support/users/xander/)
 * (@xander)
 * [20 years, 3 months ago](https://wordpress.org/support/topic/sort-by-custom-value-fields/#post-264913)
 * I think the secret is to build your own query string and bypass the wordpress
   functions intended to streamline the process… when you get into the $wpdb functions
   you can find some stuff. I ended up doing a variation of this, altering the events
   calendar plugin to display only listings in the instance that meta_value = whatever…
   works like a charm 😉
 *  [ericmcgregor](https://wordpress.org/support/users/ericmcgregor/)
 * (@ericmcgregor)
 * [20 years, 3 months ago](https://wordpress.org/support/topic/sort-by-custom-value-fields/#post-264914)
 * This is EXACTLY what I need. I’m trying to order my posts based on a custom field
   called ‘order’ and I’m giving a numberical value of 1 through 10.
 * If somebody can please, please let me know if they figured this out and how to
   implement it i would really, really appreciate it.
 * Thanks!
 *  [3stripe](https://wordpress.org/support/users/3stripe/)
 * (@3stripe)
 * [20 years, 2 months ago](https://wordpress.org/support/topic/sort-by-custom-value-fields/#post-264917)
 * I think I need to do what other people above are talking about (basically somehow
   list posts containing a custom field value of one value or another)…
 * Can anyone advise me on how to do this?
 * Many thanks
 * 3stripe
 *  [lellie](https://wordpress.org/support/users/lellie/)
 * (@lellie)
 * [20 years, 2 months ago](https://wordpress.org/support/topic/sort-by-custom-value-fields/#post-264918)
 * There is some discussion but it’s quite confusing.. a plugin which added this
   feature to query posts would be fabulous.
 *  [3stripe](https://wordpress.org/support/users/3stripe/)
 * (@3stripe)
 * [20 years, 2 months ago](https://wordpress.org/support/topic/sort-by-custom-value-fields/#post-264919)
 * Sounds good!
 * In the meantime I found a great article after 2 hours of googling – [http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query](http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query)
 * !!!

Viewing 15 replies - 1 through 15 (of 17 total)

1 [2](https://wordpress.org/support/topic/sort-by-custom-value-fields/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/sort-by-custom-value-fields/page/2/?output_format=md)

The topic ‘Sort by custom value fields?’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * 17 replies
 * 8 participants
 * Last reply from: [Xander](https://wordpress.org/support/users/xander/)
 * Last activity: [19 years, 8 months ago](https://wordpress.org/support/topic/sort-by-custom-value-fields/page/2/#post-264932)
 * Status: not a support question

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
