Forums

Sort by custom value fields? (18 posts)

  1. Xander
    Member
    Posted 6 years ago #

    I wonder if the query_posts() function has the ability to orderby a custom value field?

  2. marksbrain
    Member
    Posted 6 years ago #

    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?

  3. marksbrain
    Member
    Posted 6 years ago #

    Bueller?

  4. chujiu
    Member
    Posted 6 years ago #

    hehe

  5. marksbrain
    Member
    Posted 6 years ago #

    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?

  6. jehiah
    Member
    Posted 6 years ago #

    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

  7. marksbrain
    Member
    Posted 6 years ago #

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

  8. marksbrain
    Member
    Posted 6 years ago #

    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?

  9. marksbrain
    Member
    Posted 6 years ago #

    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?

  10. Xander
    Member
    Posted 6 years ago #

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

  11. lellie
    Member
    Posted 6 years ago #

    I'd love to know how this works too. :( no one seems to know.

  12. Xander
    Member
    Posted 6 years ago #

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

  13. ericmcgregor
    Member
    Posted 6 years ago #

    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!

  14. 3stripe
    Member
    Posted 6 years ago #

    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

  15. lellie
    Member
    Posted 6 years ago #

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

  16. 3stripe
    Member
    Posted 6 years ago #

    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

    !!!

  17. Minus3
    Member
    Posted 5 years ago #

    In the meantime I found a great article after 2 hours of googling

    Thanks, 3stripe. This article proved exactly what I needed. I wanted to be able to sort my posts, not categories. Eventually I came up with the idea to add a custom field named 'volgnummer' and sort the posts by the value of that field.

    My query now looks like this:

    $pageposts = $wpdb->get_results("
    SELECT
    wposts.*
    FROM
    $wpdb->posts wposts,
    $wpdb->postmeta wpostmeta
    WHERE
    wposts.ID = wpostmeta.post_id
    AND
    wpostmeta.meta_key = 'volgnummer'
    ORDER BY
    wpostmeta.meta_value ASC", OBJECT);
    if ($pageposts) : foreach ($pageposts as $post): setup_postdata($post);
    // Yada yada yada
    endforeach;
    endif

    Hopefully this is of any use.

  18. Xander
    Member
    Posted 5 years ago #

    Thanks for posting up that link! I found a way to get this working quite well.

    I adapted this solution to do something else entirely: select posts from a given category based on two seperate meta field values. I have a list of reviews on my site that I wanted to display by "Year" AND "Rating" but the nature of the postmeta table made this seem impossible... eventually, I put this query together:

    SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta, $wpdb->post2cat wpost2cat WHERE wposts.ID = wpostmeta.post_id AND wposts.ID = wpost2cat.post_id AND wpost2cat.category_id = '6' AND wposts.ID IN (SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'Rating' AND wpostmeta.meta_value > '7') AND wpostmeta.meta_key = 'Year' AND wpostmeta.meta_value >= '2002' AND wposts.post_status = 'publish' AND wposts.post_date < NOW() ORDER BY wposts.post_title ASC

    The subquery allowed me to select posts that were for releases that came out after the year 2002 with a rating higher than 7, for example. Maybe this technique would be of use to someone else out there. Anyway, thanks for the tip on this one!

Topic Closed

This topic has been closed to new replies.

About this Topic