meta query - Sorting not working with get_posts

I have been looking at this for hours but can't get it to work.$newsItems2 = get_posts(["post_type" =>

I have been looking at this for hours but can't get it to work.

 $newsItems2 = get_posts([
                            "post_type" => "post",
                            "post_status"      => "publish",
                            "posts_per_page" => 3,
                            "orderby"          => "date",
                            "order"            => "DESC",
                            'meta_query' => [
                                [
                                    'key' => 'news__type',
                                    'value' => 'general',
                                    'compare' => '='
                                ]
                            ]
                        ]);

The query works but it refuses to sort on publication date. What am I overlooking here? (when I remove the meta_query part it works fine! Am I trying to so something impossible?

By the way - this code is running on a taxonomy page, but that should not be an issue should it?

I have been investigating this further, have turned this into WP_query with the same parameters and get the same result. It is not sorting it! This is de SQL it is performing and indeed it is completely ignoring the orderby clause. Why?

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) 
WHERE 1=1 AND ( ( wp_postmeta.meta_key = 'news__type' AND wp_postmeta.meta_value = 'general' ) ) 
AND wp_posts.post_type = 'post' 
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'private') 
GROUP BY wp_posts.ID 
ORDER BY wp_posts.menu_order ASC LIMIT 0, 3

I have been looking at this for hours but can't get it to work.

 $newsItems2 = get_posts([
                            "post_type" => "post",
                            "post_status"      => "publish",
                            "posts_per_page" => 3,
                            "orderby"          => "date",
                            "order"            => "DESC",
                            'meta_query' => [
                                [
                                    'key' => 'news__type',
                                    'value' => 'general',
                                    'compare' => '='
                                ]
                            ]
                        ]);

The query works but it refuses to sort on publication date. What am I overlooking here? (when I remove the meta_query part it works fine! Am I trying to so something impossible?

By the way - this code is running on a taxonomy page, but that should not be an issue should it?

I have been investigating this further, have turned this into WP_query with the same parameters and get the same result. It is not sorting it! This is de SQL it is performing and indeed it is completely ignoring the orderby clause. Why?

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) 
WHERE 1=1 AND ( ( wp_postmeta.meta_key = 'news__type' AND wp_postmeta.meta_value = 'general' ) ) 
AND wp_posts.post_type = 'post' 
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'private') 
GROUP BY wp_posts.ID 
ORDER BY wp_posts.menu_order ASC LIMIT 0, 3
Share Improve this question edited May 27, 2020 at 16:56 thegirlinthecafe asked May 27, 2020 at 15:23 thegirlinthecafethegirlinthecafe 561 silver badge9 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 0

Hello bro try this will work !

$newsItems2 = get_posts( [
    "post_type"      => "post",
    "post_status"    => "publish",
    "posts_per_page" => 3,
    "orderby"        => "date",
    "order"          => "DESC",
    "meta_key"         => "news__type",
    "meta_value"       => "general",
 ] );

I figured it out, just posting it here for anyone else having this kind of problem: there was a too greedy pre_posts function firing that always changed the sorting order to menu_order. Changed that and now it's working great.

Try adding 'suppress_filters' => true as a part of the query. There may be a filter operating on the query that is replacing the modifying the order.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742394737a4435714.html

相关推荐

  • meta query - Sorting not working with get_posts

    I have been looking at this for hours but can't get it to work.$newsItems2 = get_posts(["post_type" =>

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信