This question already has an answer here:

I am using the post to display attorney verdicts and case details. The post title is the verdict amount entered as a string. (no $ "," or ".")

I am formatting the title using PHP number format:

EX: User enters 360137 in the post title.
    $number = get_the_title(); 
    $ammount = number_format($number);

    echo '<h1>$'. $ammount . '</h3>';

Returns $360,137

This is working as expected.

Using get_posts() I can 'orderby' => 'title' and 'order' => 'DESC' or 'ASC'

$args = array( 
'category' => 64, 
'orderby' => 'title', 
'order' => 'DESC' ); 

$myposts = get_posts( $args ); 
foreach ( $myposts as $post ) : setup_postdata( $post );

While this is working it does not recognize the length of the value. For example it will display 30,000 before 2,100,000. How do I get this list sorted by ASC or DESC AND by string length?

I want to show the largest verdicts first.





marked as duplicate by s_ha_dum Feb 5 '14 at 18:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I agree this is a duplicate and I did not find the above mentioned post. What is proper procedure. Delete this post? – MagRat Feb 5 '14 at 18:39
  • It can be closed as duplicate – s_ha_dum Feb 5 '14 at 18:41

It might be easier if you rearranged how you're going about this. Rather than putting a numerical value for the title, why not put it as a custom field. You can still show all your posts and orderby your custom field as a numerical value using meta_value_num. More info.

Here's a topic on the WordPress Support forums for someone trying to accomplish the same thing: http://wordpress.org/support/topic/orderby-title-doesnt-sort-numbers-correctly?replies=38

Please note what user esmi says:

Can I point out that:


correct from a programming pov? Those "numbers" are actually strings - not numeric integers.

PS -- echo '<h1>$'. $ammount . '</h3>'; should be echo '<h1>$'. $ammount . '</h1>';

  • I did catch and correct the H1 tag issue. Thank you. – MagRat Feb 5 '14 at 18:39
  • Here's something that might get you going int he right direction, if you're deadset on using numbers as the post title: fldtrace.com/wordpress/custom-post-types-numeric-title-order – NW Tech Feb 5 '14 at 19:37
  • Thank you, I am not set on doing it that way. The meta_value_num should work great. I do want to avoid using a custom post type unless there is a good reason. – MagRat Feb 5 '14 at 19:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.