Shortcode to List Articles and Group by Category
// Add Shortcode
function articles_groupedby_category() {
// WP_Query arguments
$args = array(
'post_type' => array( 'articles' ),
'nopaging' => true,
'ignore_sticky_posts' => true,
'order' => 'ASC',
'orderby' => 'title',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'name',
'operator' => 'EXISTS',
),
),
);
// The Query
$article_query = new WP_Query( $args );
// The Loop
if ( $article_query->have_posts() ) {
while ( $article_query->have_posts() ) {
$article_query->the_post();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
}
add_shortcode( 'wdev_art_cat', 'articles_groupedby_category' );