最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - How to order by post_status?

programmeradmin3浏览0评论

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
Add a comment  | 

2 Answers 2

Reset to default 10

You can use 'posts_orderby' filter to change the SQL performed.

Note that:

  • using get_posts() you need to set 'suppress_filters' argument of false 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

发布评论

评论列表(0)

  1. 暂无评论