自定义字段排序在WordPress中无法正常工作
问题描述:
我想按自定义字段对循环进行排序,但它不能正常工作。 像我有数据1200,1300,1400,1500,12500,13500通常它应该这样排序,但现在我看到这样的1200,12500,1300,13500,1400,1500。请看我的代码,让我知道在哪里我错了。自定义字段排序在WordPress中无法正常工作
代码:
<?php
$args = array(
'post_status' => 'publish',
'post_type' => 'car',
'car_type' => 'suv',
'meta_key' => 'per_month_rent',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => -1
// Several more arguments could go here. Last one without a comma.
);
?>
<div style="display: block;clear: both;">
<?php
// Query the posts:
$obituary_query = new WP_Query($args);
// Loop through the obituaries:
while ($obituary_query->have_posts()) : $obituary_query->the_post();
// Echo some markup
$image = wp_get_attachment_image_src(get_field('image'), 'best-seller-size');
$type=get_field('car_type');
$company=get_field('company_name');
include 'featured_list.php';
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
</div>
在此先感谢。
答
这是因为您正在订购数字值为字符串。
更改您的订单来自:
'orderby' => 'meta_value'
要:
'orderby' => 'meta_value_num' # Observe _num
谢谢您的回答,我已经找到了解决办法,但你是第一个,你的答案是正确的,这就是为什么我选择这个回答。 –