Support » Themes and Templates » Sort by custom value fields?

Viewing 15 replies - 1 through 15 (of 17 total)
  • 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?

    Bueller?

    hehe

    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.)

    …and the Get Custom Field Values Plugin

    But how it would actually work is beyond me.

    Anyone who knows more than me care to give a brother a hand?

    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

    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!)

    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.

    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?

    In the discussion section of the codex page mentioned above, 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?

    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

    (@lellie)

    I’d love to know how this works too. 🙁 no one seems to know.

    Xander

    (@xander)

    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

    (@ericmcgregor)

    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!

    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

    There is some discussion but it’s quite confusing.. a plugin which added this feature to query posts would be fabulous.

    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

    !!!

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Sort by custom value fields?’ is closed to new replies.