4

我在下面有这段代码,它基本上创建了 4 个链接,让我可以在前端对帖子进行排序。

        <div class="sort">
            Sort projects by:
            <a href="http://mydomain.com/find-work/" >Latest Projects</a>
            <a href="http://mydomain.com/find-work/?order=asc&orderby=date" >Ending Soon</a>
            <a href="http://mydomain.com/find-work/?order=asc&orderby=meta_value_num&meta_key=proj_budget" >Budget Low</a>
            <a href="http://mydomain.com/find-work/?order=desc&orderby=meta_value_num&meta_key=proj_budget" >Budget High</a>
        </div>

        <?php   $my_query = new WP_Query( array( 
                        'post_type' => 'project',
                        'orderby' => get_query_var('orderby'),
                        'order' => get_query_var('order'),
                        ));      
                while ( $my_query->have_posts() ) : $my_query->the_post(); ?>

第二个链接,按日期排序工作正常,但按元值排序的两个链接不起作用。我显然在我的查询中遗漏了一些东西,但我一生都无法解决。

有任何想法吗??

4

2 回答 2

8

元值有点神奇:

$my_query = new WP_Query( array( 
                    // 'post_type' => 'project',
                    'meta_key' => 'proj_budget',
                    'orderby' => 'meta_value_num'
                    ));      

codex 中解释了所有可能的值:http: //codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

于 2012-06-19T13:10:48.253 回答
8

这很简单:

new WP_Query( array( 
              //I used meta_value_num below, because it's about a numeric field
              //if you don't have a numeric field, just use meta_value
              "orderby" => 'meta_value_num',
              "meta_key" => 'price',
              "order" => 'DESC'
              ));
于 2012-11-21T22:49:25.987 回答