I am having trouble getting pagination working for custom taxonomy archive with the custom query. The pagination on the taxonomy archive ends up with a 404 page.
I have to run a query multiple times on many areas, so I have created a method for that.
Query Method
/**
* @param bool $post_type
* @param bool $posts_per_page
* @param bool $paged
* @param array $extra_args
*
* @return \WP_Query
*/
public function module_query( $post_type = FALSE, $posts_per_page = FALSE, $paged = FALSE, array $extra_args = [] ) {
// basic arguments
$args = [
'post_type' => $post_type ? $post_type : self::get_module_cpt(),
'posts_per_page' => $posts_per_page ? $posts_per_page : - 1,
'orderby' => 'title',
'order' => 'ASC',
];
// set paged
if ( $paged ) {
$args[ 'paged' ] = $paged;
}
$args = wp_parse_args( $extra_args, $args );
// init meta query var
$meta_query = [];
$meta_query[] = [
'relation' => 'AND',
[
'key' => 'permit_roles',
'compare' => 'NOT EXISTS',
],
[
'key' => 'permit_users',
'compare' => 'NOT EXISTS',
],
];
$meta_query[] = [
'relation' => 'AND',
[
'key' => 'permit_roles',
'value' => '',
'compare' => '=',
],
[
'key' => 'permit_users',
'value' => '',
'compare' => '=',
],
];
// init permit roles var
$permit_roles = [];
$r = 0;
// loop through each roles and prepare array for meta query
foreach ( $this->get_current_user_roles() as $current_user_role ) {
$permit_roles[ $r ][ 'key' ] = 'permit_roles';
$permit_roles[ $r ][ 'value' ] = sprintf( ':"%s";', $current_user_role );
$permit_roles[ $r ][ 'compare' ] = 'LIKE';
$r ++;
}
$meta_query[] = [
'relation' => 'OR',
$permit_roles,
[
'key' => 'permit_users',
'value' => sprintf( ':"%s";', get_current_user_id() ),
'compare' => 'LIKE',
],
];
// add meta query relation if more than one query
if ( count( $meta_query ) > 1 ) {
$meta_query[ 'relation' ] = 'OR';
}
// add meta query only for non admin users
if ( ! current_user_can( 'administrator' ) ) {
$args[ 'meta_query' ] = $meta_query;
}
// return the query object
return new WP_Query( $args );
}
taxonomy.php Query
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$the_query = $cp->module_query(
$cp::get_resource_cpt(), cp_get_addons_per_page(), $paged,
[
'orderby' => 'publish_date',
'tax_query' => [
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => [ $term->slug ],
'operator' => 'IN',
],
]
);
...
// while loop
...
// pagination
$cp->bootstrap_pagination( $the_query );
// reset query
wp_reset_postdata();
Pagination function
The pagination is working fine on the Custom Post time page with a custom loop.
function bootstrap_pagination( \WP_Query $wp_query = NULL, $echo = TRUE ) {
if ( NULL === $wp_query ) {
global $wp_query;
}
$pages = paginate_links( [
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $wp_query->max_num_pages,
'type' => 'array',
'show_all' => FALSE,
'end_size' => 3,
'mid_size' => 1,
'prev_next' => TRUE,
'prev_text' => __( '« Prev' ),
'next_text' => __( 'Next »' ),
'add_args' => FALSE,
'add_fragment' => '',
]
);
if ( is_array( $pages ) ) {
$pagination = '<div class="row"><div class="col mt-5"><div class="pagination d-flex justify-content-center"><ul class="pagination pagination-sm">';
foreach ( $pages as $page ) {
$pagination .= '<li class="page-item' . ( strpos( $page, 'current' ) !== FALSE ? ' active' : '' ) . '"> ' . str_replace( 'page-numbers', 'page-link', $page ) . '</li>';
}
$pagination .= '</ul></div></div></div><!-- end pagination -->';
if ( $echo ) {
echo $pagination;
} else {
return $pagination;
}
}
return NULL;
}
page_num
,pg
, etc. query string in the pagination URL. Butpaged
is reserved for the main query and it would result in the 404 error when the value is greater than the max number of pages for the main query.