• 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.

Viewing 5 replies - 1 through 5 (of 5 total)
  • romuloctba

    (@romuloctba)

    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

    romuloctba

    (@romuloctba)

    look at this: https://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

    Thread Starter curlybracket

    (@veganist)

    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();
    ?>

    Thread Starter curlybracket

    (@veganist)

    Hello, this topic should be in “advanced” not in localhost installs… don’t know why my post ended up here.

    romuloctba

    (@romuloctba)

    mine didn’t work yet >:(

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘meta_query order_by meta_value_num does not like FLOAT numbers’ is closed to new replies.