Created
July 3, 2017 12:54
-
-
Save mahdi-alavi/b50e89e9d1564e581a7b228c91872428 to your computer and use it in GitHub Desktop.
WP_Query orderby include Argument for tax_query (taxonomy)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let’s presume we have a ‘product’ post type, with a ‘names’ taxonomy. | |
/* The posts_orderby filter. | |
=========================================================================== */ | |
function itl_edit_posts_orderby( $orderby_statement, $wp_query ) { | |
if ( isset( $wp_query->query['orderby'] ) && 'include' == $wp_query->query['orderby'] ) { | |
$include = implode( ',', array_map( 'absint', $wp_query->query['include'] ) ); | |
$orderby_statement = "FIELD( term_taxonomy_id, $include )"; | |
} | |
return $orderby_statement; | |
} | |
add_filter( 'posts_orderby', 'itl_edit_posts_orderby', 10, 2 ); | |
/* WP_Query | |
=========================================================================== */ | |
$ids = array( 28, 27, 30, 29, 22 ); | |
$args = array( | |
'post_type' => 'product', | |
'post_status' => 'publish', | |
'posts_per_page' => 10, | |
'orderby' => 'include', | |
'include' => $ids, | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'names', | |
'field' => 'term_id', | |
'terms' => $ids, | |
), | |
), | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, Thanks for your help !
I modified your code to add sub terms after his parent term like this :