简体   繁体   中英

Wordpress wp_query orderby not working

I am trying to get ordering working in wp_query, but posts are still being ordered with default settings (just tag__in is working). SQL query for posts looks like this:

string(379) "SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (81) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 3" 

Here is code snippet:

remove_all_filters('posts_orderby');
$tag = get_term_by('name', 'title_post', 'post_tag');
$args=array(
            'order'=>'ID',
            'orderby'=>'ASC',
            'tag__in' => $tag,
            'posts_per_page'=>3, // Number of related posts to display.
            'caller_get_posts'=>1
);

$my_query = new wp_query( $args );
var_dump($my_query->request);

Thanks!

I have checked your code you have to pass wrong arguments.

Can you please check below code?

Wrong

'order'=>'ID',
'orderby'=>'ASC',

Right

'order'=>'ASC',
'orderby'=>'ID',

好的,我切换了 order 和 orderby ... 所以,正确的参数是 'orderby'=>'ID','order'=>'ASC',

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM