如何通过自定义字段对"query_posts"函数进行排序,同时通过另一个自定义字段来限制帖子 [英] How to sort a 'query_posts' function by custom field, while limiting posts by another custom field

查看:303
本文介绍了如何通过自定义字段对"query_posts"函数进行排序,同时通过另一个自定义字段来限制帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下功能查询WP中的一系列帖子:

I'm querying a series of posts in WP with the following function:

<?php 
$thirtydays = date('Y/m/d', strtotime('+30 days'));
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( array( 
    'post_type' => array('post', 'real-estate'), 
    'meta_key' => 'Time          Available', 
    'meta_compare' => '<=', 
    'meta_value' => $thirtydays, 
    'paged' => $paged )); 
?>

这部分工作正常.基本上,这拉动了我所有的房地产职位,但只返回了可用时间"为30天或更短的结果.

This part is working fine. It's basically pulling all my Real Estate posts, but only returning results that have a 'Time Available' of 30 days or less.

我还需要使用另一个自定义字段价格"中的数据,以从低到高的升序对帖子进行排序.

I need this to also order the posts in ascending order from low to high using the data from another custom field, 'Price.'

每当我添加标准'orderby' => 'meta_value', 'meta_key' => 'Price'时,它都不会在30天内显示结果.

Whenever I add the standard 'orderby' => 'meta_value', 'meta_key' => 'Price' it no longer shows results within 30 days.

有什么办法可以将两者结合起来吗?是否可以添加一个按钮来重新运行查询并按价格,卧室等进行排序?还是对WP来说太具体了?

Is there any way I can combine these two? And is it possible to add a button which re-runs the query and sorts by Price, Bedrooms, etc? Or is this too specific for WP?

推荐答案

我相信这会为您提供所需的信息.这是一个名为PostsOrderedByMetaQuery的类,该类扩展了WP_Query并接受新的参数'orderby_meta_key'和'orderby_order':

I believe this will provide you want you need. It's a class called PostsOrderedByMetaQuery that extends WP_Query and accepts new arguments 'orderby_meta_key' and 'orderby_order':

class PostsOrderedByMetaQuery extends WP_Query {
  var $posts_ordered_by_meta = true;
  var $orderby_order = 'ASC';
  var $orderby_meta_key;
  function __construct($args=array()) {
    add_filter('posts_join',array(&$this,'posts_join'),10,2);
    add_filter('posts_orderby',array(&$this,'posts_orderby'),10,2);
    $this->posts_ordered_by_meta = true;
    $this->orderby_meta_key = $args['orderby_meta_key'];
    unset($args['orderby_meta_key']);
    if (!empty($args['orderby_order'])) {
      $this->orderby_order = $args['orderby_order'];
      unset($args['orderby_order']);
    }
    parent::query($args);
  }
  function posts_join($join,$query) {
    if (isset($query->posts_ordered_by_meta)) {
      global $wpdb;
      $join .=<<<SQL
INNER JOIN {$wpdb->postmeta} postmeta_price ON postmeta_price.post_id={$wpdb->posts}.ID
       AND postmeta_price.meta_key='{$this->orderby_meta_key}'
SQL;
    }
    return $join;
  }
  function posts_orderby($orderby,$query) {
    if (isset($query->posts_ordered_by_meta)) {
      global $wpdb;
      $orderby = "postmeta_price.meta_value {$this->orderby_order}";
    }
    return $orderby;
  }
}

您会这样称呼它:

$thirtydays = date('Y/m/d', strtotime('+30 days'));
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = new PostsOrderedByMetaQuery(array(
  'post_type' => array('post', 'real-estate'),
  'meta_key' => 'Time Available',
  'meta_compare' => '<=',
  'meta_value' => $thirtydays,
  'paged' => $paged,
  'orderby_meta_key' => 'Price',
  'orderby_order'    => 'DESC',
));
foreach($query->posts as $post) {
  echo " {$post->post_title}\n";
}

您可以将PostsOrderedByMetaQuery类复制到主题的functions.php文件中,也可以在您正在编写的插件的.php文件中使用它.

You can copy the PostsOrderedByMetaQuery class to your theme's functions.php file, or you can use it within a .php file of a plugin you may be writing.

如果您想快速进行测试,我已经发布了 该代码的独立版本 到Gist,您可以将其下载为test.php并复制到Web服务器的根目录,针对您的用例进行修改,然后使用http://example.com/test.php这样的URL从浏览器进行请求.

If you want to test it quickly I've posted a self-contained version of the code to Gist which you can download and copy to your web server's root as test.php, modify for your use case, and then request from your browser using a URL like http://example.com/test.php.

希望这会有所帮助.

-迈克

P.S.这个答案是 与我刚刚在WordPress Answers 上给出的答案非常相似,这是StackOverflow的姊妹站点,很多像我这样的WordPress爱好者每天都在回答问题.您可能想要看到也有答案,因为它有更多解释,而且您可能想查看 WordPress答案.希望以后您也考虑在那里上发布WordPress问题吗?

P.S. This answer is very similar to an answer I just gave over at WordPress Answers, which is the sister site of StackOverflow where lots of WordPress enthusiasts like me answer questions daily. You might want to see that answer too because it has a tad more explanation and because you might want to see WordPress Answers. Hope you'll consider posting your WordPress questions over there too in the future?

这篇关于如何通过自定义字段对"query_posts"函数进行排序,同时通过另一个自定义字段来限制帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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