我有一个自定义post_type
叫special_listing
其中包含一个自定义字段被称为listing_index
。 A special_listing
,其中listing_index
为20,应该出现在索引为15,然后为10,依此类推之前。然而,这似乎不起作用,列表以他们选择的顺序出现。为什么不是这个Wordpress查询排序正确
我不是一个PHP或WordPress的家伙,我主要处理ASP.Net和C#,所以这对我来说有点混乱。我正在犯一些简单的错误吗?
功能:
// Get the first $count listings with the highest indices for a given $region_slug:
function get_listings($region_slug, $count) {
$args = array(
'post_type' => 'special_listing',
'meta_key' => 'listing_region_slug',
'meta_value' => $region_slug
);
$posts = get_posts($args);
sort_array_on_field($posts, 'listing_index', 'DESC');
truncate_array($posts, $count);
return $posts;
}
查看:
<?php
$listings = get_listings(get_microsite_slug() . '-microsite-home-featured', 4);
$i = 0;
if (sizeof($listings) > 0) : while ($i < sizeof($listings)) : $listing = $listings[$i]; // Loop and set current listing
?>
<section>
<a href="<?php echo $listing->destination; ?>">
<h3><?php echo $listing->post_title; ?></h3>
<p><?php echo $listing->post_content; ?></p>
</a>
</section>
<?php $i++; ?>
<?php endwhile; ?>
<?php endif; ?>
我尝试:
我不知道如何返回listing_index
值
function get_sorted_listings($region_slug, $count){
$args = array(
'post_type' => 'kodakalaris_listing',
'meta_query' => array(
'relation' => 'AND',
array (
'key' => 'listing_region_slug',
'value' => $region_slug
),
array (
'key' => 'listing_index',
'value' => ''
),
orderby: 'listing_index',
order: 'DESC'
)
);
$posts = get_posts($args);
truncate_array($posts, $count);
return $posts;
}
更新
尝试一种新方法,现在它将内容按发布日期进行抽取。仍然没有订购listing_index
,但至少它不是完全随机的。我开始寻找到meta_query。这不会只返回一个结果与一个单一的值?我也尝试过this other SO answer,但我发现它的实现混乱。
<?php
$args = array(
'post_type' => 'kodakalaris_listing',
'meta_key' => 'listing_region_slug',
'meta_value' => get_microsite_slug() . '-microsite-home-featured',
'posts_per_page' => 4,
'order' => 'DESC',
'orderby' => 'listing_index'
);
$listings = new WP_Query($args);
if ($listings->have_posts()) : while ($listings->have_posts()) : $listings->the_post();
?>
...
...
<?php
endwhile;
endif;
?>
感谢您的评论,但您的答案似乎缺少对'meta_value_num'应该引用的'meta_key'的调用。除非我走了,否则我不知道。无论如何,我已经更新了我的问题。你可以看一下吗? – itsclarke