I'm using get_posts()
like this:
$posts = get_posts(array('orderby' => 'post_status'))
which is not possible cause it's not in the allowed keys in the parse_orderby()
method
How to get around this limitation?
I'm using get_posts()
like this:
$posts = get_posts(array('orderby' => 'post_status'))
which is not possible cause it's not in the allowed keys in the parse_orderby()
method
How to get around this limitation?
Share Improve this question asked Jul 3, 2015 at 9:19 XaverXaver 1,0393 gold badges13 silver badges29 bronze badges 1- look at meta_key and meta_query in orderby. wordpress.stackexchange/questions/15477/… – Amirmasoud Commented Jul 3, 2015 at 10:05
2 Answers
Reset to default 10You can use 'posts_orderby'
filter to change the SQL performed.
Note that:
- using
get_posts()
you need to set'suppress_filters'
argument offalse
for the filter to be performed - if you don't explicitly set
'post_status'
you'll get only published posts (so no much to order)
Code sample:
$filter = function() {
return 'post_status ASC';
};
add_filter('posts_orderby', $filter);
$posts = get_posts('post_status' => 'any', 'suppress_filters' => false);
remove_filter('posts_orderby', $filter);
add_filter('posts_orderby', 'custom_post_status', 10,2);
function custom_post_status($args, $wp_query){
if($wp_query->query_vars['orderby'] == 'post_status') {
if($wp_query->query_vars['order']) {
return 'post_status '.$wp_query->query_vars['order'];
}
else {
return 'post_status ASC';
}
}
return $args;
}
Adding this would enable 'orderby' => 'post_status' in WP_Query