admin管理员组文章数量:1122832
I've checked around and haven't seen an answer which works as of yet. I have a WP_Query with the following arguments:
$args = array(
'post_status' => 'publish',
'post_type' => 'listing',
'meta_key' => 'client_feedback_score',
'orderby' => 'client_feedback_score',
'order' => 'DESC'
);
$query = new WP_Query($args);
I want to order the results by the custom post field client_feedback_score
, lowest to highest. But this doesn't seem to work... can anyone point me in the right direction?
EDIT (SOLVED):
Thanks to Milo's response, here's the working code for ordering by a numerical meta value:
$args = array(
'post_status' => 'publish',
'post_type' => 'listing',
'meta_key' => 'client_feedback_score',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
I've checked around and haven't seen an answer which works as of yet. I have a WP_Query with the following arguments:
$args = array(
'post_status' => 'publish',
'post_type' => 'listing',
'meta_key' => 'client_feedback_score',
'orderby' => 'client_feedback_score',
'order' => 'DESC'
);
$query = new WP_Query($args);
I want to order the results by the custom post field client_feedback_score
, lowest to highest. But this doesn't seem to work... can anyone point me in the right direction?
EDIT (SOLVED):
Thanks to Milo's response, here's the working code for ordering by a numerical meta value:
$args = array(
'post_status' => 'publish',
'post_type' => 'listing',
'meta_key' => 'client_feedback_score',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
Share
Improve this question
edited Jan 22, 2020 at 17:43
Marc
7315 silver badges15 bronze badges
asked Oct 4, 2011 at 15:10
Adam MossAdam Moss
1,0622 gold badges12 silver badges18 bronze badges
0
3 Answers
Reset to default 100orderby
should be meta_value_num
, or meta_value
, not the name of the key. See WP_Query orderby parameters.
if your meta value is not numeric for example if it is a date value; than you can add parameter meta_type => DATE
.
Possible values are; 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'
.
Query with meta type will be like that:
$args = array(
'post_status' => 'publish',
'post_type' => 'listing',
'meta_key' => 'client_feedback_score',
'orderby' => 'meta_value_num',
'meta_type' => 'DATE',
'order' => 'DESC'
);
To address the point raised by @adamj on the accepted answer
if the meta_key does not exist for the post yet, the post will be ignored.
Here is a solution that will include even posts which are missing a client_feedback_score
value. You can setup a simple query all listing posts. Then manually left join it to the postmeta
table where meta_value
is client_feedback_score
. Then sort it with an orderby
clause.
$args = array(
'post_status' => 'publish',
'post_type' => 'listing',
);
add_filter('post_clauses','post_meta_sort',10,1);
function post_meta_sort( $sql_clauses ) {
global $wpdb;
remove_filter( 'posts_clauses', 'post_meta_sort' ); // Stop this function from running on other queries.
$sql_clauses['join'] .= "LEFT JOIN $wpdb->postmeta AS sort_score_postmeta ON $wpdb->posts.ID = sort_score_postmeta.post_id
AND sort_score_postmeta.meta_key = 'client_feedback_score'";
$sql_clauses['orderby'] = 'sort_score_postmeta.meta_value+0 DESC';
return $sql_clauses;
}
$query = new WP_Query($args);
本文标签: custom fieldWPQueryOrder results by meta value
版权声明:本文标题:custom field - WP_Query - Order results by meta value 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736305430a1932588.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论