0

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'    => '$',
) );
7
  • How is the amount stored in DB? PS. Your code is so messy, OMG... It's really very hard to read it... (For example: why do you use this query_posts( $query ); in there?) Nov 26, 2018 at 21:22
  • It stores them under wp_postmeta with the metakey deal_amount with the amount that was typed it. For example one transactions has a meta_value of 350,00. As for my query_posts( $query ); what would you recommend I use?
    – bigant841
    Nov 26, 2018 at 23:03
  • 1
    Try setting orderby to meta_value_num, rather than meta_value. But you really shouldn't be storing numbers with commas in them like that. 6,000 should be stored as 6000. If you want to display them with a comma, do that later on the front-end with number_format(), but don't store them like that. Nov 27, 2018 at 14:36
  • Regarding query_posts(), in you code it's doing nothing. You should just remove that line. Nov 27, 2018 at 14:36
  • @JacobPeattie I updated my code and I am still not getting this to display properly. How do you recommend I go about using number_format()?
    – bigant841
    Nov 27, 2018 at 14:44

1 Answer 1

0

With the guidance of @jacobPeattie below is the working example. Once I removed the commas everything was working great. I added number_format to the deal amount to automatically add the commas on the frontend.

<?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>$' . number_format($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; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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