WordPress.org

Ready to get started?Download WordPress

Forums

can we get custom field values as numeric values? (2 posts)

  1. Dave Wright
    Member
    Posted 3 years ago #

    Is it possible to retreive custom vield values as numeric data rather than a string?
    I'm working on a real estate site and I'd like to set up a query that only shows posts with more than 3 bedrooms or that costs less than £200,000. The number of bedrooms and sale values are stored in custom fields and it seems that by default I get these values as strings so that instead of things going in the order...

    1,2,4,7,10,30,60
    it would instead go...
    1,10,2,30,4,60,7

    Anyone have any work-arounds for this?

  2. vtxyzzy
    Member
    Posted 3 years ago #

    The simplest thing to do is pad the numbers on the left with zeros to have them sort in numeric order:

    0001
    0002
    0010
    0030
    ...

    If you are writing your own sql for the query, you can adjust it to convert the string to a number for sorting. For example, if your 'ORDER BY' statement is this:

    ORDER BY meta_value

    you can change it to this:

    ORDER BY (meta_value + 0)

    Adding zero forces sql to convert to a number.

Topic Closed

This topic has been closed to new replies.

About this Topic