I am trying to sort a list of post based of the meta_key deal amount, which would be to display the highest deal first and work its way down to smaller deal amounts. The deal amount would be anywhere from 1,000 to 9,000,000. Currently the below code is sorting the deal amounts based on the first number. So if I have a transaction that is 6,000 and 6,000,000 they put them side by side instead of displaying the 6,000,000 first. The deal amount gets pulled from a custom MetaBox within CMB2. Is there something I am missing within my code that is not the sorting is not recognizing the amount after the comma?
My WP_Query
<?php
$transaction = new WP_Query( array(
'post_type' => 'transactions',
'paged' => $paged,
'posts_per_page' => 50,
'orderby' => array( 'meta_value_num' => 'ASC' ),
'meta_key' => 'deal_amount',
) );
if ( $transaction->have_posts() ) : ?>
<?php
while ( $transaction->have_posts() ) : $transaction->the_post();
$deal_amount = get_post_meta( get_the_ID(), 'deal_amount', true );
$property_type = get_post_meta( get_the_ID(), 'property_type', true );
$property_size = get_post_meta( get_the_ID(), 'property_size', true );
?>
<article class="col">
<div class="trans-content">
<?php the_title('<h3>', '</h3>'); ?>
<?php echo '<h4>$' . esc_html($deal_amount) . '</h4>'; ?>
<?php echo esc_html($property_type); ?><br>
<?php echo esc_html($property_size); ?><br>
<?php $terms_as_text = get_the_term_list( $post->ID, 'loan-type', '', ', ', '' ) ; echo strip_tags($terms_as_text); ?></p>
</div>
<?php edit_post_link('edit'); ?>
</article>
<?php endwhile; wp_reset_postdata(); ?>
<?php else : endif; ?>
The MetaBox
add_action( 'cmb2_admin_init', 'transactions_metabox' );
function transactions_metabox() {
$prefix = 'transactions_';
$cmb = new_cmb2_box( array(
'id' => $prefix . 'transactions',
'title' => esc_html__( 'Transactions', 'cvcapital' ),
'object_types' => array( 'transactions', ),
) );
$cmb->add_field( array(
'name' => 'Deal Amount',
'id' => 'deal_amount',
'type' => 'text',
'before_field' => '$',
'column' => true, // Display field value in the admin post-listing columns
'before_display' => '$',
) );
query_posts( $query );
in there?)orderby
tometa_value_num
, rather thanmeta_value
. But you really shouldn't be storing numbers with commas in them like that.6,000
should be stored as6000
. If you want to display them with a comma, do that later on the front-end withnumber_format()
, but don't store them like that.query_posts()
, in you code it's doing nothing. You should just remove that line.