Forums

Sort posts by custom field numeric value (10 posts)

  1. ravenseye
    Member
    Posted 4 months ago #

    I'm trying to customize a template category page that sorts posts within a certain category by the natural numeric order specified in a custom field. I have a realtor's website, we're using a post for each listing, and they want to have all the listing posts sort by price. I've created a custom field called 'price' and typed a number into each field.

    Using the instructions here http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query I'm able to do this, mostly, but the posts are not in natural numeric order. The custom field value is a string, and PHP does not sort the values numerically by default.

    I'm a bit of a novice with PHP, but very experienced with custom templates. Please help...

  2. t31os_
    Member
    Posted 4 months ago #

    Can i see what you have so far?

    I can help expand on what you have, saving writing something from scratch..

    :)

  3. ravenseye
    Member
    Posted 4 months ago #

    Thanks for the response! I just somehow found the answer by casting about blindly on Google. I'll post it here in case it might help others. Originally, I was trying to use this code:

    $querystr = "
       SELECT wposts.*
       FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
       WHERE wposts.ID = wpostmeta.post_id
       AND wpostmeta.meta_key = 'price'
       AND wposts.post_type = 'post'
       ORDER BY wpostmeta.meta_value DESC
       ";

    The values returned by the database would not sort naturally. So, I simply changed the last line to this:

    ORDER BY wpostmeta.meta_value+0 DESC

    and now it does what I want it to. Perhaps if you see anything else I might be missing here, I'd appreciate your input... This level of hacking templates is as deep as I've gone.

  4. t31os_
    Member
    Posted 4 months ago #

    See this thread for making the code a little easier..
    http://wordpress.org/support/topic/285939?replies=7

    The information there should be relevant in helping a little...

    Hard to say regarding the sort order because i don't have your data infront of me to fiddle around with (i'm not asking for it), so i can only guess...

    In any case whenever you have data in an array you can resort that data many different ways depending on what PHP function you call...

    sort()
    ksort()
    and more...

    There's also quite good support for meta value sorting built into Wordpress if you dig through the codex.. ( i know finding specific info can sometimes be tough ).

    Since data is usually returned in an array your options are quite open...

    If you have the behaviour you want now though, great!... :)

    Have a good weekend.. ;)

  5. pl4y312
    Member
    Posted 2 months ago #

    Is there anyone know where I should put that script?

  6. cruisetastic
    Member
    Posted 2 months ago #

    I can't get them to sort beyond the FIRST number... so if there's a property $24,000 and another $399,000 they'll be sequential. Any suggestions? Here's the code I'm using:

    <?php
    $price = get_post_meta($post->ID, 'price', true);
    $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 = 14
    AND $wpdb->term_taxonomy.taxonomy = 'category'
    AND $wpdb->posts.post_status = 'publish'
    AND $wpdb->postmeta.meta_key = 'price'
    ORDER BY $wpdb->postmeta.meta_value+0 DESC
    ";

    $pageposts = $wpdb->get_results($querystr, OBJECT);

    ?>

  7. apljdi
    Member
    Posted 2 months ago #

    I can't get them to sort beyond the FIRST number... so if there's a property $24,000 and another $399,000 they'll be sequential.

    I don't think I understand. The first number should follow the second assuming no values in between, since you are ordering with DESC. What isn't sorting right? Do you get your results the other way around?

  8. saranghills
    Member
    Posted 1 month ago #

    My client wants the posts to be sorted with a custom number value. As you all have said, it counts 10 as 1. Can anyone tell me how to make WP understand that 10 is 10, not 1 ?

    Here is the code that I am using, if it will be of any help:

    query_posts('cat=3&orderby=title&meta_key=Post_order&order=ASC&showposts=-1');

    I tried searching for a solution, but couldn't find any good results.

  9. apljdi
    Member
    Posted 1 month ago #

    Did you try ravenseye's trick?

    ORDER BY wpostmeta.meta_value+0 DESC

  10. cassio
    Member
    Posted 5 days ago #

    I changed the file query.php to see which custom field was passed.

    If the field is numeric, I use the solution below:
    $orderby = "$wpdb->postmeta.meta_value+0"

    Otherwise I use the default Wordpress:
    $orderby = "$wpdb->postmeta.meta_value"

    But now I face a very similar problem: if the field is a date, what do I do?

Reply

You must log in to post.

About this Topic