wp query – WordPress Pagination changes my template

Question

I have a problem using pagination for terms in my custom taxonomy, let’s call it Case-Studies.
If i go to /insights/page/2, posts will be displayed correctly, and I can navigate between pages.
The problem comes when accessing /insights/case-studies/page/2, which will actually show me the posts from /insights/page/2 ( the content gets redirected, and also, /insights/case-studies/page/2 will access archive-insights instead of taxonomy-insights.
/insights/case-studies works fine, but when adding a pagination, all content shown will be actually the content from root (/insights/page/2)

I have Insights custom post type, defined here:

function create_insights_posttype() {
register_post_type(
    'insights',
    array(
        'label' => 'Insights',
        'labels'              => array(
            'name'          => __( 'Insights', 'x' ),
            'singular_name' => __( 'Insights', 'x' ),
            'search_items'  => __( 'Search Insights', 'x' ),
            'add_new_item'  => __( 'Add Insight', 'x' ),
            'edit_item'     => __( 'Edit Insight', 'x' ),
            'update_item'   => __( 'Update Insight', 'x' ),
            'new_item_name' => __( 'New Insight', 'x' ),
        ),
        'public'              => true,
        'has_archive'         => 'insights',
        'exclude_from_search' => true,
        'publicly_queryable'  => true,
        'menu_icon'           => 'dashicons-open-folder',
        'rewrite'             => array(
            'slug' => 'insights/%insights-category%',
            'with_front' => false,
        ),
        'show_in_rest'        => true,
        'supports'            => array(
            'title',
            'author',
            'excerpt',
            'editor',
            'thumbnail',
            'results',
            'page-attributes',
            'custom-fields',
            'revisions',
            'post-formats',
        ),
    )
);
}

add_action( 'init', 'create_insights_posttype' );

with custom taxonomy defined here

function insights_taxonomy() {
$labels = array(
    'name'                       => _x( 'Categories', 'taxonomy general name', 'x' ),
    'singular_name'              => _x( 'Category', 'taxonomy singular name', 'x' ),
    'search_items'               => __( 'Search categories', 'x' ),
    'popular_items'              => __( 'Popular categories', 'x' ),
    'all_items'                  => __( 'All categories', 'x' ),
    'parent_item'                => null,
    'parent_item_colon'          => null,
    'edit_item'                  => __( 'Edit category', 'x' ),
    'update_item'                => __( 'Update category', 'x' ),
    'add_new_item'               => __( 'Add New category', 'x' ),
    'new_item_name'              => __( 'New category', 'x' ),
    'separate_items_with_commas' => __( 'Separate category with commas', 'x' ),
    'add_or_remove_items'        => __( 'Add or remove categories', 'x' ),
    'choose_from_most_used'      => __( 'Choose from the most used categories', 'x' ),
    'menu_name'                  => __( 'Categories', 'x' ),
);

register_taxonomy(
    'insights-category',
    array( 'insights' ),
    array(
        'hierarchical'          => true,
        'rewrite'             => array(
            'slug' => 'insights',
            'with_front' => false,
        ),
        'labels'                => $labels,
        'show_ui'               => true,
        'show_in_rest'          => true,
        'show_admin_column'     => true,
        'update_count_callback' => '_update_post_term_count',
        'query_var'             => true,
        'publicly_queryable'    => true,
        'show_tagcloud'         => false,
    )
);

}

add_action( ‘init’, ‘insights_taxonomy’ );

In my functions, I defined a filter for %insights-category% :

function wpa_show_permalinks_insights( $post_link, $post ){
    if ( is_object( $post ) && $post->post_type == 'insights' ){
        $terms = wp_get_object_terms( $post->ID, 'insights-category' );
        if( $terms ) {
            return str_replace( '%insights-category%' , $terms[0]->slug , $post_link );
        }
        else {
            return str_replace( '%insights-category%' , 'all' , $post_link );
        }

    }
    return $post_link;
}

add_filter( 'post_type_link', 'wpa_show_permalinks_insights', 1, 2 );

For taxonomy-insights, I defined a WP_QUERY that supports pagination, like here:

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = null;
$query = new WP_Query( array(
    'post_type' => 'insights',
    'posts_per_page' => 2,
    'paged' => $paged,
    'tax_query'      => array(
        array(
            'taxonomy' => 'insights-category',
            'field'    => 'slug',
            'terms'    => 'case-studies',
        ),
    ),
) );

in progress 0
Calin Diea 2 weeks 2021-11-12T06:43:14-05:00 0 Answer 0 views 0

Answer ( 1 )

    0
    2021-11-15T12:24:13-05:00

    Your URL structure won’t work. Based on your structure, if you look at /insights/abc, is abc an Insight or an Insights Category? You can’t tell, and neither can WordPress. You need a URL structure than distinguishes between insights and categories.

Leave an answer

Browse
Browse