WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] 2.7.1 query_posts meta_compare bug?! (4 posts)

  1. ttk_2k
    Member
    Posted 5 years ago #

    With 2.7.1, for example, assign following to a post:

    meta_key: number
    meta_value: 5

    now you do query_posts():

    query_posts('meta_key=number&meta_compare=>=&meta_value=100');

    The above will list the post which you have assigned a meta_value 5.

    After spending many hours on this issue, I finally find out that 'meta_compare' is treating 'meta_value' as "alphabets", not as values. It looks 'meta_compare' thinks 99 is 'larger' than 100 because the later one starts with '1' while the first one starts with '9'.

    Is this a bug?! If not then the related part of this document should be revised: http://codex.wordpress.org/Template_Tags/query_posts

  2. MichaelH
    Member
    Posted 5 years ago #

    Fixed!

    http://codex.wordpress.org/Template_Tags/query_posts#Custom_Field_Parameters

    If you want something like that to work, might use 001,002,078,099,100 for values. Ugly that is!

    Thanks for the heads-up.

  3. ttk_2k
    Member
    Posted 5 years ago #

    Great!
    Thanks for clearing it up, Michael! :)

  4. ttk_2k
    Member
    Posted 5 years ago #

    A little tip:

    if you wish to output/echo these number strings without seeing leading zeros, you can use the number_format() function, like:

    echo number_format(get_post_meta($post->ID, 'YOUR_META_KEY', true));

Topic Closed

This topic has been closed to new replies.

About this Topic