我正在尝试对基于meta\\u键交易金额的帖子列表进行排序,这将首先显示最高的交易,然后逐步降低到较小的交易金额。交易金额将在1000至900万之间。目前,下面的代码正在根据第一个数字对交易金额进行排序。因此,如果我有一个6000和6000000的交易,他们会将它们并排放置,而不是先显示6000000。交易金额从CMB2内的自定义元数据库中提取。我的代码中是否缺少了排序无法识别逗号后的金额的内容?
我的WP\\U查询
<?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; ?>
代谢箱
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\' => \'$\',
) );
SO网友:bigant841
在@jacobPeattie的指导下,下面是一个工作示例。一旦我去掉逗号,一切都很好。我在交易金额中添加了number_格式,以便在前端自动添加逗号。
<?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; ?>