I have a custom post type named "testimonials" with a slug of reviews
. I also have a taxonomy named "testimonial-department" with a slug of reviews
. The goal being I will have pretty permalinks such as /reviews/sales/
or /reviews/service/
.
Everything is working as I would like, with the exception of pagination. I have tried all of the many different tips and tricks to get the pagination to work as expected, but no matter what /reviews/sales/page/2
just doesn't work. However, if I manually enter /reviews/sales/?page=2
it leads to the second page of results just as one would expect. Is there a rewrite rule that I can use that will allow me to use the query string ?page=2
but rewrite it as /page/2/
or is there a way to force WordPress to use the non-pretty permalink with the page number as a query string with the normal pagination?
This is what my taxonomy and custom post type registration looks like, plus I have a rewrite rule in place for posts.
add_action( 'init', 'create_testimonial_department_taxonomy' );
function create_testimonial_department_taxonomy() {
$labels = array(
'name' => 'Testimonial Department',
'singular_name' => 'Testimonial Department',
'search_items' => 'Search Testimonial Departments',
'all_items' => 'All Testimonial Departments',
'edit_item' => 'Edit Testimonial Department',
'update_item' => 'Update Testimonial Department',
'add_new_item' => 'Add New Testimonial Department',
'new_item_name' => 'New Testimonial Department',
'menu_name' => 'Testimonial Department',
'view_item' => 'View Testimonial Department',
'not_found' => 'No Testimonial Departments found'
);
register_taxonomy(
'testimonial-department',
'testimonials',
array(
'label' => __( 'Testimonial Departments' ),
'hierarchical' => false,
'labels' => $labels,
'public' => true,
'show_in_nav_menus' => true,
'meta_box_cb' => false,
'show_tagcloud' => false,
'show_admin_column' => true,
'query_var' => true,
'sort' => true,
'rewrite' => array(
'slug' => 'reviews',
'hierarchical' => false
)
)
);
}
function aw_testimonials_post_type() {
// Set UI labels for Testinomials post type
$labels = array(
'name' => _x( 'Testimonials', 'Post Type General Name'),
'singular_name' => _x( 'Testimonial', 'Post Type Singular Name'),
'menu_name' => __( 'Testimonials'),
'parent_item_colon' => __( 'Testimonials Parent'),
'all_items' => __( 'All Testimonials'),
'view_item' => __( 'View Testimonial'),
'add_new_item' => __( 'Add New Testimonial'),
'add_new' => __( 'Add New'),
'edit_item' => __( 'Edit Testimonial'),
'update_item' => __( 'Update Testimonial'),
'search_items' => __( 'Search Testimonial'),
'not_found' => __( 'Testimonial Not Found'),
'not_found_in_trash' => __( 'Testimonial Not found in Trash'),
);
// Set other options for Testimonials post type
$args = array(
'label' => __( 'Testimonials'),
'description' => __( 'Posts specifically for displaying testimonials.'),
'labels' => $labels,
'rewrite' => array('slug' => 'reviews/%testimonial-department%'),
'query_var' => true,
'menu_icon' => 'dashicons-megaphone',
'supports' => array('title','thumbnail'),
'taxonomies' => array('post_tag', 'testimonial-department'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 14,
'can_export' => true,
'has_archive' => 'reviews',
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
);
// Registering your Custom Post Type
register_post_type( 'testimonials', $args );
}
add_action( 'init', 'aw_testimonials_post_type', 0 );
function testimonial_department_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'testimonial-department' );
if( $terms ){
return str_replace( '%testimonial-department%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'testimonial_department_post_link', 1, 3 );
function testimonials_posts_per_page( $query ) {
if (is_admin()) {
return $query;
}
if ( $query->is_main_query() ) {
if ( is_post_type_archive( 'testimonials' ) ) {
$query->set( 'posts_per_page', 6 );
return $query;
}
else if ( is_tax( 'testimonial-department' ) ) {
$query->set( 'posts_per_page', 6 );
$query->set('post_type','testimonials');
return;
}
}
}
add_action( 'pre_get_posts', 'testimonials_posts_per_page', 1 );