WordPress使用数组按价格按升序合并排序,但价格与0必须显示在最后

时间:2015-02-26 作者:user3510525

我使用下面的代码过滤页面上的查询,并按价格升序对帖子进行排序。我得到了期望的输出,但我担心的是,我希望所有价格最终都为0。

<?php
global $wp_query;
$args = array_merge( $wp_query->query_vars, array( \'orderby\' => \'meta_value_num\',\'meta_key\' => \'price\',\'order\' => \'ASC\'));
query_posts( $args );
?>
请帮帮我伙计们。。。提前感谢

整页代码如下

<?php
/**
* The template for displaying Archive pages.
*
* Learn more: http://codex.wordpress.org/Template_Hierarchy
*
* @package progression
*/

get_header(\'cars\'); ?>
<?php get_template_part( \'page\', \'vehicle-title\' ); ?>
<div class="width-container">

<?php if ( have_posts() ) : ?>

<div id="page-title">
<h1 id="page-heading">
<?php if (is_tax(\'vehicle_type\')) {
$term = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) );
$tax_term_breadcrumb_taxonomy_slug = $term->taxonomy;
echo \'\' . $term->name . \'\';
}
?>
</h1>
</div><!-- close #page-title -->

<div id="4-content-container" class="tax-vehicle-container">
<?php echo do_shortcode(\'[vehicle_searchform include="make,model"]\') ?> 

    <?php
    global $wp_query;
    $args = array_merge( $wp_query->query_vars, array( \'orderby\' => \'meta_value_num\',\'meta_key\' => \'price\',\'order\' => \'ASC\'));
    query_posts( $args );
 ?>



<?php
/* Start the Loop */
$count = 1;
$count_2 = 1;
?>

<?php while ( have_posts() ) : the_post();
if($count >= 5) { $count = 1; }
?>

<div class="grid4column-progression <?php if($count == 4): echo \' lastcolumn-progression\'; endif; ?>">
<?php
get_template_part( \'content\', \'vehicle\');
?>

</div>
<?php if($count == 4): ?><div class="clearfix"></div><?php endif; ?>
<?php $count ++; $count_2++; endwhile; ?>

<div class="clearfix"></div>
<?php show_pagination_links( ); ?>

</div><!-- close #content-container -->

<?php else : ?>

<?php get_template_part( \'no-results\', \'inventory\' ); ?>

</div><!-- close #content-container -->
<?php endif; ?>

<?php get_sidebar( \'vehicle\' ); ?>
</div><!-- close .width-container -->
<?php get_footer(); ?>

1 个回复
SO网友:user3510525

希望这对某些人有用。

<?php
/* ADDED by for sorting ASCENDING order but call for price (ie price with 0) last */
function my_sort_custom( $orderby, $query ){
    global $wpdb;
    $orderby =  "   case when CAST(meta_value AS SIGNED) = \'0\' then 1 else 0 end ,CAST(meta_value AS SIGNED)   ";
    return  $orderby;
}
add_filter(\'posts_orderby\',\'my_sort_custom\',10,2);
global $wp_query;
$args = array_merge( $wp_query->query_vars, array(\'meta_key\' => \'price\'));

query_posts( $args );
?>

结束

相关推荐

How do i Sort my Posts

我寻找一种解决方案,如何在我的页面中对文章进行排序。我在谷歌上搜索了几个小时,尝试了很多东西,但都不管用:(。不管怎样,也许有人能给我一个好的建议。我想要一个简单的列表,其中包含排序选项,如“最新优先”或“相关性”或“字母顺序(条目标题)”。有什么想法吗?