Skip to content

Instantly share code, notes, and snippets.

@khleomix
Last active March 16, 2022 18:51
Show Gist options
  • Save khleomix/800f681a78ede046bad0a73eb21162ae to your computer and use it in GitHub Desktop.
Save khleomix/800f681a78ede046bad0a73eb21162ae to your computer and use it in GitHub Desktop.
Order posts by taxonomy
<?php
/**
* Show all posts based on taxonomy meta key.
*
* @return WP_Query $courses New WP_Query containing courses.
*/
function display_posts_taxonomy_order() {
$term_args = array(
'taxonomy' => 'discipline',
'meta_compare' => 'NUMERIC',
'meta_key' => 'course_order', // custom meta field added to custom taxonomy.
'orderby' => 'meta_value_num',
'order' => 'ASC',
);
$course_terms = get_terms( $term_args );
foreach ($course_terms as $course_term) :
$term_slug = $course_term->slug;
$the_query = new WP_Query( array(
'post_type' => 'post',
'orderby' => 'menu_order title',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'discipline',
'field' => 'slug',
'terms' => $term_slug,
),
),
));
if( $the_querys->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<article class="hentry" id="post-' . get_the_ID() . '">';
echo '<h3>' . get_the_title() . '</h3>';
echo '<div class="post-description">' . wpautop( get_the_content() ) . '</div>';
echo '</article>';
endwhile;
endif;
wp_reset_postdata();
endforeach;
endif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment