Last active
June 29, 2021 17:58
-
-
Save addisonhall/6683663572c0999226c62309b8869d28 to your computer and use it in GitHub Desktop.
WP_Query: Orderby custom fields
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
<?php | |
$args = array( | |
'post_type' => 'custom_post_name' // including the post type name seems to make this not work! | |
'meta_key' => 'custom_field_name', | |
'orderby' => 'meta_value', | |
'order' => 'ASC', | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'custom_tax_name', | |
'field' => 'term_id', | |
'terms' => array( 1, 2, 3 ), | |
), | |
), | |
); | |
$query = new WP_Query( $args ); |
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
<?php | |
// leaving out the post_type parameter made it all good again | |
$args = array( | |
'meta_key' => 'custom_field_name', | |
'orderby' => 'meta_value', | |
'order' => 'ASC', | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'custom_tax_name', | |
'field' => 'term_id', | |
'terms' => array( 1, 2, 3 ), | |
), | |
), | |
); | |
$query = new WP_Query( $args ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment