$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Debugging wp_query orderby for taxonomy|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Debugging wp_query orderby for taxonomy

matteradmin11PV0评论

I cannot seem to find where WP_Query adds the orderby clause to a SQL query.

I am using this code:

function wpd_team_taxonomy_queries( $query ) {
if ( !is_admin() && $query->is_tax( 'team' ) && $query->is_main_query() ) {
    $query->set( 'orderby',  " SUBSTRING_INDEX( t.name, ' ', -1 ) "); 
    $query->set( 'order', 'ASC' );
}
}add_action( 'pre_get_posts', 'wpd_team_taxonomy_queries' );

This works well apart from one particular taxonomy type, which I am debugging. I can see that WP_Query orderby parameter is set as above, but I cannot see in any of the queries from the DEBUG plugin or the Query Monitor plugin where the SUBSTRING_INDEX( t.name, ' ', -1 ) code is added to a SQL query.

Neither for the correctly ordering results nor the one that is not ordered.

How would I find where this particular SQL query is built for orderby to debug this ?

I cannot seem to find where WP_Query adds the orderby clause to a SQL query.

I am using this code:

function wpd_team_taxonomy_queries( $query ) {
if ( !is_admin() && $query->is_tax( 'team' ) && $query->is_main_query() ) {
    $query->set( 'orderby',  " SUBSTRING_INDEX( t.name, ' ', -1 ) "); 
    $query->set( 'order', 'ASC' );
}
}add_action( 'pre_get_posts', 'wpd_team_taxonomy_queries' );

This works well apart from one particular taxonomy type, which I am debugging. I can see that WP_Query orderby parameter is set as above, but I cannot see in any of the queries from the DEBUG plugin or the Query Monitor plugin where the SUBSTRING_INDEX( t.name, ' ', -1 ) code is added to a SQL query.

Neither for the correctly ordering results nor the one that is not ordered.

How would I find where this particular SQL query is built for orderby to debug this ?

Share Improve this question asked Feb 14, 2019 at 20:48 KolleykKolleyk 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 2

You can't pass SQL directly as query parameters. The only valid values for orderby are covered in the docs for WP_Query.

If you want to directly modify the SQL, you need to use the posts_* filters, in this case posts_orderby.

Post a comment

comment list (0)

  1. No comments so far