如何通过元值获取帖子顺序?

时间:2015-04-30 11:38:54

标签: php wordpress

好吧,我被困在wordpress的排序帖子中!我知道这是一个先前提出的问题,但我没有运气,我在这里找到了代码和解决方案! 以下是我正在使用的代码段。

$args = array(
      'showposts'=>10,
      's' => $search,
      'meta_key'        => 'neighboorhood',
      'meta_value'  => $location,
      'orderby'   => 'meta_value_num',
      'meta_key'  => 'is_sort',
    );

3 个答案:

答案 0 :(得分:0)

这是行不通的,因为你在数组中使用了两次meta_key。您应该将meta_query用于where子句,并将meta_key仅用于排序。

$args = array(
    'showposts'=>10,
    's' => $search,
    'meta_query' => array(

        array(
            'key' => 'neighboorhood',
            'value' => $location
        )
    ),
    'meta_key'  => 'is_sort',
    'orderby'   => 'meta_value_num'
);

答案 1 :(得分:0)

I hope this is solution of your problem

$args= query_posts(
    array(  'post_type' => 'your post type',
            'order'     => 'ASC',
            'meta_key' => 'some_key',
            'orderby'   => 'meta_value', //or 'meta_value_num'
            'meta_query' => array(
                                array('key' => 'order_in_archive',
                                      'value' => 'some_value'
                                )
                            )
    )
);

答案 2 :(得分:0)

使用下面提到的代码按meta_value获取帖子顺序:

<?php 

        $myargs = array(
           'posts_per_page' => 4, //number of post to show
           's' => $search,
           'meta_key' => 'is_sort', //name of meta field
           'orderby' => 'meta_value_num', 
           'order' => 'ASC', // you can modify it as per your use
           'meta_query' => array(
                array(
                    'key'       => 'neighboorhood',
                    'value'     => $location,
                    )
                )
        );

        query_posts($myargs );

        if (have_posts()) : while (have_posts()) : the_post(); ?>

         <h2><?php the_title();?></h2>
         <p><?php the_content();?></p>

         <?php endwhile; 
           endif; 
           wp_reset_query();  ?>

只需使用上述代码,您将获得所需的结果。如果您在日期字段中使用此过程,请在此处查看以便更好地理解:http://www.wptricks24.com/how-to-order-wordpress-post-by-custom-field-date