我在使用自定义查询为自定义分类法存档进行分页时遇到问题。分类法归档上的分页以404页结束。
我必须在许多区域上多次运行查询,因此我为此创建了一个方法。
查询方法
/**
* @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 );
}
分类法。php查询
$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();
分页功能在使用acustom循环的自定义Post time页面上分页工作正常。
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;
}