admin管理员组文章数量:1123504
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' => '$',
) );
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' => '$',
) );
Share
Improve this question
edited Nov 27, 2018 at 14:41
bigant841
asked Nov 26, 2018 at 20:51
bigant841bigant841
17211 bronze badges
7
|
Show 2 more comments
1 Answer
Reset to default 0With 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; ?>
本文标签: Wp Query sort order from custom MetaBox
版权声明:本文标题:Wp Query sort order from custom MetaBox 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736577911a1944897.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
query_posts( $query );
in there?) – Krzysiek Dróżdż Commented Nov 26, 2018 at 21:22orderby
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. – Jacob Peattie Commented Nov 27, 2018 at 14:36query_posts()
, in you code it's doing nothing. You should just remove that line. – Jacob Peattie Commented Nov 27, 2018 at 14:36