如何按元值获取帖子顺序? [英] How to fetch posts order by meta value?

查看:24
本文介绍了如何按元值获取帖子顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我被困在 wordpress 的分类帖子中!我知道这是以前提出的问题,但我在这里找到的代码和解决方案对我来说没有运气!以下是我正在使用的代码片段.

Well I am stuck in a sorting posts in wordpress! I know this is a previously asked question, but no luck for me the codes and solutions I found here! The below is code snippet I am using.

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

推荐答案

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

Just use below mentioned code to fetch posts order by 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

Just use the above mentioned code, you will get your desired result. If you are using this process in case of date field, then please check here for better understanding : http://www.wptricks24.com/how-to-order-wordpress-post-by-custom-field-date

这篇关于如何按元值获取帖子顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆