wordpress 使用数组合并按价格升序排序,但带有 0 的价格必须显示在最后

问题描述 投票:0回答:2

我使用下面的代码来过滤页面上的查询并按价格升序对帖子进行排序。我得到了期望的输出,但我担心的是,我希望所有价格最后都是 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(); ?>
php wordpress sorting
2个回答
0
投票

如果您的帖子数量很少,那么快速而肮脏的事情就是遍历循环两次,在第一次通过时抑制零,并在第二次通过时抑制除零之外的所有内容:

for ($i=0, $i++, $i<=1) :
   while (have_posts()) : the_post();
   $show_non_zeros = ($i === 0) && ( get_post_meta( get_the_ID(), 'price', true) > 0); 
   $show_zero_price = ($i === 1) && ( get_post_meta( get_the_ID(), 'price', true) == 0); 
   if ($show_non_zeros || $show_zero_price) {
      //write output
   }
   end while;
end for;

如果您正在寻找更快/更优雅的解决方案,您可以在调用 usort()

 和循环之间 
$posts
 
query_posts


0
投票

希望这对某人有用..我这样做了

<?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 );
    ?>
© www.soinside.com 2019 - 2024. All rights reserved.