Pagination URL in Custom Content Type

I have some custom content types on my WordPress website. In order to use pagination I am using wppagenavi plugin and it works fine except in a single custom content type page template that I create a custom query to show the related custom content types. It shows the pagination links correctly like below:

Single Research Field Post:

http://localhost/research-field/reports/

Url to the second page created by wppagenavi:

http://localhost/research-field/reports/page/2

But when I click on the second page it redirects to the first page:

http://localhost/research-field/reports/

I searched a lot for this problem and looked at many SE questions on this subject but none of them worked for me, until I tried to try passing the page parameter using query variables like:

http://localhost/research-field/reports/?page=2

And this successfully redirected me to http://localhost/research-field/reports/2 and successfully showed the second page. What is confusing me is that it works well on other template files (archives, archive custom content types, etc).

Here is how I set up the custom content type:

add_action( 'init', 'create_research_field_post_type' );
function create_research_field_post_type() {
    register_post_type( 'research-field',
        array(
            'labels' = $labels,
            'public' = true,
            'has_archive' = false,
            'show_ui' = true,
            'show_in_menu' = true,
            'rewrite' = array('slug' = 'research-field','with_front' = true),
            'supports' = array( 'title', 'editor', 'thumbnail', 'excerpt' ),
            'publicly_queryable' = true,
            'query_var' = true,
        )
    );
}


add_action( 'init', 'create_publications_post_type' );
function create_publications_post_type() {
    register_post_type( 'publication',
        array(
            'labels' = $labels,
            'public' = true,
            'has_archive' = true,
            'show_ui' = true,
            'show_in_menu' = true,
            'publicly_queryable' = true,
            'supports' = array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
        )
    );
}

And here is my single custom content type template (single-research-field.php):

?php
global $paged;
if (get_query_var('paged')) {
    $paged = get_query_var('paged');
} elseif (get_query_var('page')) {
    $paged = get_query_var('page');
} else {
    $paged = 1;
}

$p = get_post();

$fields_meta_query = array(
    'key' = 'research_fields',
    'value' = '"'.$p-ID.'"',
    'compare' = 'LIKE'
);
$publications_query = new WP_Query(array(
    'post_type' = 'publication',
    'paged' = $paged,
    'meta_query' = array(
        'relation' = 'AND',
        $fields_meta_query
    )
));

// And the pagination

?php wp_pagenavi(array( 'query' = $publications_query ) ); ? 

Topic plugin-wp-pagenavi rewrite-rules pagination custom-post-types Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.