WP Query on Order and OrderBy is not working

 ̄綄美尐妖づ 提交于 2021-02-11 18:14:26

问题


I have a problem with my code, it seems that my order/orderby is not working on my WP Query, when i set the order value to DESC, its not changing still ASC when i check on wpdb lastquery.

Here's my code:

$args = array( 
                                    'post_status' => 'publish',
                                    'posts_per_page' => $per_page, 
                                    'paged' => $paged,
                                    'orderby'=> 'date',
                                    'order'=> $sortBy,
                                    'posts_per_page' => $per_page,
                                    'post_type'=> 'product',
                                    'tax_query' => array(
                                        array(
                                            'taxonomy'      => 'product_cat',
                                            'field' => 'term_id', //This is optional, as it defaults to 'term_id'
                                            'terms'         => $catby,
                                            'operator'      => 'IN', // Possible values are 'IN', 'NOT IN', 'AND'.
                                            'suppress_filters' => false
                                        )
                                    ),
                                    'date_query' => array(
                                        array(
                                            'after' => $rangeby
                                        )
                                    )
                                ); 

But when i use this simple args DESC order is working:

                        $args = array( 
                            'post_status' => 'publish',
                            'posts_per_page' => $per_page,
                            'paged' => $paged,
                            'orderby'=> 'date',
                            'order'=> 'desc',
                            'suppress_filters' => true,
                            'post_type'=> 'product' 
                        );

Can someone help me about this? Thanks!


回答1:


I would start by echoing the value of $sortBy, in the example that works you're not using that variable, so it probably not returning the value 'date'.



来源:https://stackoverflow.com/questions/59206460/wp-query-on-order-and-orderby-is-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!