WordPress.org

Ready to get started?Download WordPress

Forums

meta_query order_by meta_value_num does not like FLOAT numbers (6 posts)

  1. curlybracket
    Member
    Posted 1 year ago #

    Hello,

    I am developing a complex theme, where users may sort posts based on a meta_key (which is a price).

    My prices resemble this kind of values : "4,90", "4,35", "5,15".

    The piece of code i wrote works well with one exception : the sorting omits the value after the comma.
    Writing this, i guess the problem here may be the following : prices are in french, not in english, so where you would use a dot in english, most european countries use a comma.
    How would i be able to fix this without replacing every comma in my database ? Is there a possibility i could do a str_replace() while executing my code ?

    Here is my code :

    <?
    if ($_GET['o'] == "price_desc") {
                        $args = array(
                            'posts_per_page' => '9',
                            'cat' => $current,
                            'paged' => $paged,
                            'meta_key' => 'price',
                            'orderby' => 'meta_value_num',
                            'order'=>'DESC',
                        );
    }
    $myposts = new WP_Query($args);
                    while ($myposts->have_posts()) : $myposts->the_post();
                        get_template_part('content_post');
                    endwhile;
                    wp_reset_postdata();
    ?>

    $current is the current category id

    Any help appreciated.

    PS : My WP install is using fr_FR as WP_LANG.

  2. romuloctba
    Member
    Posted 1 year ago #

    lol, doing that sux, right? i have the same problem but i'm pt_BR and we use currency like yours.
    Well, the closest I got so far is
    'orderby' => 'meta_value meta_value_num', (yes, it does take multiple parameters)
    but it works for about 10 or 20 results, and then it messes up again :S

  3. romuloctba
    Member
    Posted 1 year ago #

    look at this: http://alanaimoveis.com.br/imovel-para-venda/ thats how it is doing :( the code I use is
    `<?php $argsvs = array(
    'post_type' => 'imovel-para-venda',
    'meta_key' => 'wpcf-imovel-preco',
    'posts_per_page' => 0,
    'orderby' => 'meta_value meta_value_num',
    'order' => 'ASC'
    );`
    edit: if i only use meta_value_num, it does not sort in any kind, it messes everything up

  4. curlybracket
    Member
    Posted 1 year ago #

    Romuloctba ! Thank you so much ! This actually works perfectly :)
    BUT : it works only when using ASC for sort_order, not DESC.

    Here is the modified code for future reference :

    <?
    if ($_GET['o'] == "price_desc") {
                        $args = array(
                            'posts_per_page' => '9',
                            'cat' => $current,
                            'paged' => $paged,
                            'meta_key' => 'price',
                            'orderby' => 'meta_value_num meta_value',
                            'order'=>'ASC',
                        );
    }
    $myposts = new WP_Query($args);
                    while ($myposts->have_posts()) : $myposts->the_post();
                        get_template_part('content_post');
                    endwhile;
                    wp_reset_postdata();
    ?>
  5. curlybracket
    Member
    Posted 1 year ago #

    Hello, this topic should be in "advanced" not in localhost installs... don't know why my post ended up here.

  6. romuloctba
    Member
    Posted 1 year ago #

    mine didn't work yet >:(

Topic Closed

This topic has been closed to new replies.

About this Topic