I'm querying a custom post type and displaying all posts by terms (basically posts by category) as follows, everything is working fine EXCEPT for some reason no matter how I write the tax_query array, I can't seem to get the order of the terms to change (ASC and DESC don't change anything).
Can anyone see where I might have gone wrong here?
$temp_query = $wp_query;
$custom_terms = get_terms('instruction_categories');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array(
'post_type' => 'instruction-sheets',
'orderby' => 'name', // order of the products
'order' => 'ASC',
'hide_empty' => 1,
'tax_query' => array(
array(
'taxonomy' => 'instruction_categories',
'field' => 'slug',
'terms' => $custom_term->slug,
'hide_empty' => 1,
'orderby' => $custom_term->name,
'order' => 'ASC', // switching to DESC should reverse order, but doesnt
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo 'Region '.$custom_term->name.'';
while($loop->have_posts()) : $loop->the_post();
I'm querying a custom post type and displaying all posts by terms (basically posts by category) as follows, everything is working fine EXCEPT for some reason no matter how I write the tax_query array, I can't seem to get the order of the terms to change (ASC and DESC don't change anything).
Can anyone see where I might have gone wrong here?
$temp_query = $wp_query;
$custom_terms = get_terms('instruction_categories');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array(
'post_type' => 'instruction-sheets',
'orderby' => 'name', // order of the products
'order' => 'ASC',
'hide_empty' => 1,
'tax_query' => array(
array(
'taxonomy' => 'instruction_categories',
'field' => 'slug',
'terms' => $custom_term->slug,
'hide_empty' => 1,
'orderby' => $custom_term->name,
'order' => 'ASC', // switching to DESC should reverse order, but doesnt
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo 'Region '.$custom_term->name.'';
while($loop->have_posts()) : $loop->the_post();
Share
Improve this question
asked Apr 3, 2019 at 19:17
TheSurferTheSurfer
111 silver badge2 bronze badges
2 Answers
Reset to default 1For anyone having the same issue, here's how I ended up solving this. I installed the WP Term Order plugin and then used the following code, the magic happens in the 'orderby' => 'order' line where the 'order' is being pulled from the drag and drop menu order functionality in the admin added by the plugin.
$custom_terms = get_terms('instruction_categories');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array(
'post_type' => 'instruction-sheets',
'orderby' => 'meta_value',
'order' => 'ASC',
'hide_empty' => 1,
'meta_key' => $custome_term->slug,
'tax_query' => array(
array(
'taxonomy' => 'instruction_categories',
'field' => 'slug',
'terms' => $custom_term->slug,
'hide_empty' => 1,
'orderby' => 'order',
// 'order' => 'DESC',
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
while($loop->have_posts()) : $loop->the_post();
Then enter whatever code you need to show for parts of your post content, and then don't forget to close the whole thing out with:
endwhile;
}
}
I believe your query arguments need revision. The value for orderby
should be meta_value
or meta_value_num
for a numeric sort.
You also need a meta_key
set in your arguments array and in your case that should be set to the value of $custom_term->slug
.
Untested arguments for your query:
$args = array(
'post_type' => 'instruction-sheets',
'orderby' => 'meta_value', // Change #1
'order' => 'ASC',
'hide_empty' => 1,
'meta_key' => $custome_term->slug,
'tax_query' => array(
array(
'taxonomy' => 'instruction_categories',
'field' => 'slug',
'terms' => $custom_term->slug,
'hide_empty' => 1,
),
),
);
WP Codex reference for sorting: https://codex.wordpress/Class_Reference/WP_Query#Order_.26_Orderby_Parameters