0

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 ) ); ?> 
  • Archive pagination and singular post pagination use different query var and rewrite patterns. Note that what worked for you was page and not paged. – Milo Jul 18 '17 at 12:36
  • @Milo I tried both of them. The problem is that before reaching in this template file WordPress redirects to the first page. – user88731 Jul 19 '17 at 12:38
  • @Milo When I remove the /page/ and use this http://localhost/research-field/reports/2 WordPress picks this template and it works with no problem – user88731 Jul 19 '17 at 12:39
  • Yes, that's what I mean, slug/2/ is the rewrite pattern for singular post pagination. /page/ redirects because that is the pattern for archives, not posts. – Milo Jul 19 '17 at 13:10
  • @Milo Aha, I got you. So my problem is that I am in a single view. I think I have to implement the pagination myself so that I can use the correct URLs. Thank you! – user88731 Jul 19 '17 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.