Opened 8 years ago
Closed 8 years ago
#2173 closed defect (bug) (invalid)
Error when WP_Query parses orderby array in function parse_order
Reported by: | oloynet | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Component: | General | Keywords: | |
Cc: |
Description
Example of request where I want to order by two custom fields in meta query
start_date_order and is_sticky
$args = array( 'no_found_rows' => true , 'posts_per_page' => 4, 'post_type' => 'event', 'post_status' => 'publish', 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'start_date_order', 'type' => 'UNSIGNED', ), array( 'key' => 'is_sticky', 'type' => 'UNSIGNED', ), ), 'orderby' => array( 'start_date_order' => 'DESC', 'is_sticky' => 'ASC', ), );
The '$primary_meta_query' var in method parse_order( $order ) is set forever with the first item of '$meta_clauses' array
see line 2336 /wp-includes/query.php
<?php $primary_meta_query = reset( $meta_clauses );
I fix the problem with the following PHP code
<?php //$primary_meta_query = reset( $meta_clauses ); $primary_meta_query = array(); foreach( $meta_clauses as $meta_clause ) { if( $meta_clause['key'] == $orderby ) { $primary_meta_query = $meta_clause; break; } }
Now the good sql query is look like :
SELECT wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) WHERE 1=1 AND ( wp_postmeta.meta_key = 'start_date_order' AND mt1.meta_key = 'is_sticky' ) AND wp_posts.post_type = 'event' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY CAST(wp_postmeta.meta_value AS UNSIGNED) DESC, CAST(mt1.meta_value AS UNSIGNED) ASC LIMIT 0, 3
Change History (1)
Note: See
TracTickets for help on using
tickets.
Hello @oloynet,
this Trac is for WordPress.org. For the software WordPress we have https://core.trac.wordpress.org/.