WP_QUERY ordered by custom field (4 posts)

  1. EddieVerso
    Posted 3 years ago #

    Hi everyone,

    I'm trying to order post by custom field "YEAR" doing this query:

    new WP_Query('cat=5&showposts=-1&meta_key=year&orderby=meta_value&order=ASC');

    unfortunately the order is incorrect. The field is a number field and there are values like 100, 500, 1050.. and so on.

    The output is a little bit uncorrect, like:

    1050 - 100 - 200 - 900 - 400

    I just don't know what could be the problem, so please help me :)

  2. Andrew Bartel
    Posted 3 years ago #

    Use meta_value_num instead of meta_value on your order by parameter for numeric values.


  3. EddieVerso
    Posted 3 years ago #

    oo tnx! Works fine!

    So i suppose that ordering by meta_value will sort the number "alfabetically" (not sure is an english word!)

    Thank you again man!!

  4. Andrew Bartel
    Posted 3 years ago #

    Yes, exactly. FWIW it's alphabetically.

    Please mark as resolved and happy coding!

Topic Closed

This topic has been closed to new replies.

About this Topic