WordPress.org

Ready to get started?Download WordPress

Forums

help me order by custom field (float and str) (7 posts)

  1. romuloctba
    Member
    Posted 1 year ago #

    Hi guys,
    Well, i need to order posts by a custom field named wpsc-imovel-preco. it is a string field. The code i have is this:

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

    The thing is, we use values like: 100.000,00 or 30.000,00 Thats because in brazil we separate thousands by . not by , and decimals by , not by .
    well, i'm trying to fix this about a week for now, but it just doesn't work.

    If you wanna se what happens, please reffer to http://alanaimoveis.com.br/imovel-para-venda/ it begins with millions, then goes to 100thousand, then works ok 'till 200thousand, and goes back to 30fcknThousand.... please... please.. help me.

    I wanned to convert to string before doing orderby, but it doesn't seem to be supported.

  2. nazzilla
    Member
    Posted 1 year ago #

    i dont understand what you want to do, but right way to call metaquery for custom order is

    $argsvs = array(
    		'post_type' => 'imovel-para-venda',
    		'posts_per_page' => -1,
    	 	'meta_query'        => array(
                'key' => 'wpcf-imovel-preco',
                'value' => 'meta_value_num',
                'compare' => '>'
            )
        );
  3. romuloctba
    Member
    Posted 1 year ago #

    First, thank you very much for awnsering... Really, that means a lot to me!
    Well, it didn't work :(
    I'm triyng to order my loop by a custom field.
    See what happened: http://i.imgur.com/PUvJB.png the custom field is the value after R$ (in white)
    Before your hint it was like this: http://i.imgur.com/eYwh6.jpg

    My loop with your code was:
    ` <?php $argsvs = array(
    'post_type' => 'imovel-para-venda',
    'posts_per_page' => -1,
    'meta_query' => array(
    'meta_key' => 'wpcf-imovel-preco',
    'meta_value' => 'meta_value_num',
    'compare' => '>'
    )
    );
    $loop = new WP_Query( $argsvs );
    while ( $loop->have_posts() ) : $loop->the_post();`

    I hope you don't give up on helping me :P

  4. romuloctba
    Member
    Posted 1 year ago #

    I wish there was a wayi could (int)$my_custom_field to make the sorting
    or even better: a way to sort by a variablle. I would work on my custom field into a nice variable with int numbers.
    I would str_replace it, taking the . and , out, then order it....

  5. nazzilla
    Member
    Posted 1 year ago #

    sorry :| try with this

    $argsvs = array(
        'post_type' => 'imovel-para-venda',
        'meta_key' => 'wpcf-imovel-preco',
        'posts_per_page' => 0,
        'orderby' => 'meta_value',
        'order' => 'ASC'
    );

    http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

  6. romuloctba
    Member
    Posted 1 year ago #

    nah, that just doensn't word either :( my field is a string

  7. nazzilla
    Member
    Posted 1 year ago #

    this work for me :( my custom field is with price but without €$r, symbols...
    you can try to order with javascript/jquery

    (function( $ ) {
    jQuery.fn.orderBy = function(keySelector,inverse){
        return this.sort(function(a,b){
            a = keySelector.apply(a);
            b = keySelector.apply(b);
    		return (
    			isNaN(a) || isNaN(b) ?
    				a > b : +a > +b
    		) ?
    		inverse ? -1 : 1 :
    		inverse ? 1 : -1;
        });
    };})( jQuery );
    
    $(".preco").orderBy(function(){
    		return +$(this).text();
    	},true);

Topic Closed

This topic has been closed to new replies.

About this Topic