Before we look at pagination, from what I have picked up in your question, you only need terms starting with A
. If so, you can make use of the name__like
parameter in get_terms
to get terms only that starts with A
. Just a note on that, the operation of the parameter was changed in Wordpress V3.7, so you will need to apply the following filter for this to work as expected (Credit to @s_ha_dum, see his answer here. NOTE: The code requires PHP 5.4+)
add_filter( 'terms_clauses', function ($clauses)
{
remove_filter( 'term_clauses', __FUNCTION__ );
$pattern = '|(name LIKE )\'%(.+%)\'|';
$clauses['where'] = preg_replace($pattern,'$1 \'$2\'',$clauses['where']);
return $clauses;
});
$terms = get_terms( 'ownwer' ['name__like' => 'A'] );
To paginate a list of terms, we need:
To get the page number of the current page or any page is easy. This will work on all pages including static front pages and single post pages
if ( get_query_var( 'paged' ) ) {
$current_page = get_query_var( 'paged' );
} elseif ( get_query_var( 'page' ) ) {
$current_page = get_query_var( 'page' );
} else {
$current_page = 1;
}
We now need to know the total amount of terms from the taxonomy. Here we can use either wp_count_terms()
, which uses get_terms()
or use get_terms()
itself. Remember, if we used the filter to get terms by a specific letter/name, the filter will be applied to this run of get_terms()
/wp_count_terms()
as well as we are making using of a closure
$terms_count = get_terms( 'owner', ['name__like' => 'A', 'fields' => 'count'] );
Now we have the total amount of terms matching our criteria. Just remember, if your are going to use wp_count_terms()
, just remember to set hide_empty
to true if you do not need to include empty terms. By default, wp_count_terms()
sets this to false
Now we can calculate our maximum amount of pages
// Set the amount of terms we need to display per page
$terms_per_page = 10;
// Get the total amount of pages
$max_num_pages = $terms_count / $terms_per_page;
To get pagination links to next and previous pages, it is really easy. The only thing a pagination function ever needs to know from any query is the maximum amount of pages, nothing else. It does not need to know what it should paginate. So we can simply use the next_posts_link()
and previous_posts_link()
functions. You can also make use paginate_links()
or even write your own function to suite your needs. Just remember to pass $max_num_pages
as the maximum amount of pages to the pagination function
next_posts_link( 'Next terms', $max_num_pages );
previous_posts_link( 'Previous terms' );
The only thing we still need to do is to paginate get_terms()
, which is also really easy. We will make use of the offset
parameter to offset the terms according to page and number
to get the required amount of terms per page
// Calculate offset
$offset = ( $current_page == 1) ? 0 : ( ($current_page - 1) * $terms_per_page );
// Setup our arguments
$args = [
'number' => $terms_per_page,
'offset' => $offset
];
We can now put everything together
ALL TOGETHER NOW
Just before we do, a couple of important notes
All of the code is untested
The code needs at least PHP 5.4, which is the absolute minimum PHP version you should be running at time of this post
Modify and tear apart as needed
Here is the code, finally
add_filter( 'terms_clauses', function ($clauses)
{
remove_filter( 'term_clauses', __FUNCTION__ );
$pattern = '|(name LIKE )\'%(.+%)\'|';
$clauses['where'] = preg_replace($pattern,'$1 \'$2\'',$clauses['where']);
return $clauses;
});
$taxonomy = 'owner';
$terms_count = get_terms( $taxonomy, ['name__like' => 'A', 'fields' => 'count'] );
if ( $terms_count && !is_wp_error( $terms ) ) {
if ( get_query_var( 'paged' ) ) {
$current_page = get_query_var( 'paged' );
} elseif ( get_query_var( 'page' ) ) {
$current_page = get_query_var( 'page' );
} else {
$current_page = 1;
}
// Set the amount of terms we need to display per page
$terms_per_page = 10;
// Get the total amount of pages
$max_num_pages = $terms_count / $terms_per_page;
// Calculate offset
$offset = ( $current_page == 1) ? 0 : ( ($current_page - 1) * $terms_per_page );
// Setup our arguments
$args = [
'number' => $terms_per_page,
'offset' => $offset,
'name__like' => 'A'
];
$terms = get_terms( $taxonomy, $args );
// Do what you need to do with $terms
next_posts_link( 'Next terms', $max_num_pages );
previous_posts_link( 'Previous terms' );
}
author
,author
is a reserved term$_GET
) variables and your own custom prev/next links.