How to order a custom post by connect taxonomy name?

[ad_1]

How to order custom post type posts by taxonomy name? I got the post loop almost working, only i want to change the amount of items per page instead the amount of terms per page and offcourse the pagination need to adjust to the amount of pages that i set.

If you have a shorter way that is also fine, iam looking for the best and easy way to do this.

// Get all the categories
$categories = get_terms( ‘document-type’ );
$paged = ( get_query_var( ‘paged’ ) ) ? get_query_var( ‘paged’ ) : 1;

$post_type = ‘documentation’;
$taxonomy = ‘document-type’;

// count the number of terms for correct pagination
$term_count = get_terms( array (
‘taxonomy’ => $taxonomy,
‘fields’ => ‘count’,
) );

// define the number of terms per page
$terms_per_page = 2;

// find out the number of pages to use in pagination
$max_num_pages = ceil( $term_count / $terms_per_page );

// get the page number from URL query
$current_page = get_query_var( ‘paged’, 1 );

// calculate offset
$offset = ( $terms_per_page * $current_page ) – $terms_per_page;

// get all taxonomy terms
$terms = get_terms( array (
‘taxonomy’ => $taxonomy,
‘order’ => ‘ASC’,
‘orderby’ => ‘name’,
‘number’ => $terms_per_page,
‘offset’ => $offset,
) );

// cycle through taxonomy terms
foreach ( $terms as $term ) {

// cycle through posts having this term
$items = get_posts( array (
‘post_type’ => $post_type,
‘tax_query’ => array(
array(
‘taxonomy’ => $taxonomy,
‘terms’ => $term->term_id,
),
),
‘posts_per_page’ => -1, // different from WP_Query (see Code Ref)
) );

// essential, see comments inside foreach() loop
global $post;

foreach ( $items as $item ) {
the_title();
}
wp_reset_postdata(); // moved outside the foreach() loop

}

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer