1

I have a template for a custom post-type single-continent.php which calls a secondary WP_Query loop to load a list of posts.

$query = new WP_Query(array(
    'meta_query'  => array($meta_queries),
    'order'       => 'DESC',
    'orderby'     => 'date',
    'paged'       => $paged,
    'post_status' => 'publish',
    'post_type'   => 'post'
));

The function is setup to use get_query_var() to check for page/paged but I cannot seem to make the URL /page/2 work. Instead I get a 301 redirect back to the continent. I can use /2 which is picked up by get_query_var('page') but not sure if this is the correct solution (and I need to be able to use this with the_posts_pagination.

2
  • refer to this previous posted answer
    – BlueSuiter
    Jan 30, 2017 at 9:41
  • That question isn't quite what I need - they are looping through news posts on a page, but I have a custom post type here. I don't want to look through continents but rather loop through posts from my continent page itself. Jan 30, 2017 at 9:45

1 Answer 1

4

Forgive me for answering my own question, but this post gave me the answer I needed: WP_Query Pagination on single-custom.php

I had to take a copy of the original wp_query

$original_query = $wp_query;

then assign my custom loop to the global $wp_query, and finally reset everything with

wp_reset_postdata();
$wp_query = $original_query;

In essence, this is what was missing:

function mytheme_template_redirect() {
    if(is_singular('continent')) {
        global $wp_query;
        $page = (int)$wp_query->get('page');
        if($page > 1) {
            $query->set('page', 1);
            $query->set('paged', $page);
        }
        remove_action('template_redirect', 'redirect_canonical');
    }
}

Called with:

add_action('template_redirect', 'mytheme_template_redirect', 0);

Everything works fine now as I'm no longer getting a 301 redirect when appending /page/2 to the (single) custom post URL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.