Skip to content

Instantly share code, notes, and snippets.

@addisonhall
Last active June 29, 2021 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save addisonhall/6683663572c0999226c62309b8869d28 to your computer and use it in GitHub Desktop.
Save addisonhall/6683663572c0999226c62309b8869d28 to your computer and use it in GitHub Desktop.
WP_Query: Orderby custom fields
<?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 );
<?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