我希望我能正确解释,所以我们开始吧!
我有一个房地产列表页面,该页面应在发布列表的日期前进行排序(数据来自API,因此它不是WP发布日期),然后任何已售出的房地产应在列表中的最后一个排序日期,即它们出售的时间,最新售出的房地产应出现在该子列表的第一个。第一个自定义字段称为listing\\u date,另一个称为agreed\\u date。
有人知道这种订货方式是否可行吗?我想我可能需要使用2个查询,但这意味着我将无法对该解决方案使用分页。这是我最近一次失败的尝试。
<?php
$args = array(
\'post_type\' => \'listing\',
\'posts_per_page\' => 10,
\'paged\' => $paged,
\'tax_query\' => array(
array(
\'taxonomy\' => \'listingcategory\',
\'field\' => \'slug\',
\'terms\' => \'commercial\',
),
),
\'orderby\' => array(
\'agreed_date\' => \'ASC\',
\'listing_date\' => \'DESC\'
),
);
$listing_posts = new WP_Query($args);
谢谢!
更新的代码:
$args = array(
\'post_type\' => \'listing\',
\'posts_per_page\' => 10,
\'paged\' => $paged,
\'tax_query\' => array(
array(
\'taxonomy\' => \'listingcategory\',
\'field\' => \'slug\',
\'terms\' => \'commercial\',
),
\'meta_query\' => array(
\'relation\' => \'AND\',
\'listing_date_clause\' => array(
\'key\' => \'listing_date\',
\'compare\' => \'EXISTS\',
),
\'agreed_date_clause\' => array(
\'key\' => \'agreed_date\',
\'compare\' => \'EXISTS\',
),
),
),
\'orderby\' => array(
\'listing_date_clause\' => \'DESC\',
\'agreed_date_clause\' => \'DESC\'
),
);
最合适的回答,由SO网友:Pim Schaaf 整理而成
根据https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters 您希望类似以下代码示例来指定它:
$args = array(
\'orderby\' => array(
\'title\' => \'DESC\',
\'menu_order\' => \'ASC\'
)
);
$query = new WP_Query( $args );
WordPress 4.0支持这一点。
对于自定义字段(post meta),您需要添加一个元查询,如https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query
E、 g:
$q = new WP_Query( array(
\'meta_query\' => array(
\'relation\' => \'AND\',
\'state_clause\' => array(
\'key\' => \'state\',
\'value\' => \'Wisconsin\',
),
\'city_clause\' => array(
\'key\' => \'city\',
\'compare\' => \'EXISTS\',
),
),
\'orderby\' => \'city_clause\',
) );
从4.2开始支持这一点。
在您的情况下(根据您的问题编辑),我认为您需要:
$args = array(
\'post_type\' => \'listing\',
\'posts_per_page\' => 10,
\'paged\' => $paged,
\'tax_query\' => array(
array(
\'taxonomy\' => \'listingcategory\',
\'field\' => \'slug\',
\'terms\' => \'commercial\',
)
),
\'meta_query\' => array(
\'relation\' => \'AND\',
\'listing_date_clause\' => array(
\'key\' => \'listing_date\',
\'compare\' => \'EXISTS\',
),
\'agreed_date_clause\' => array(
\'key\' => \'agreed_date\',
\'compare\' => \'EXISTS\',
),
),
\'orderby\' => array(
\'listing_date_clause\' => \'DESC\',
\'agreed_date_clause\' => \'DESC\'
),
);