How to paginate a custom query on the single.php file

Say you’re viewing a Custom Post type of “Events” using, “single-events.php” Paginating won’t work because the canonical redirects strip the “/page/2/ for your url. What that basically means is that WordPress tries to find the right post even if the url entered is slightly wrong. So, it strips what you need to paginate. We can override that with a simple function:

// Disable canonical redirects on the events so we can paginate  a single post view.
add_filter('redirect_canonical','simple_disable_redirect_canonical');

function simple_disable_redirect_canonical($redirect_url) {
if (is_singular('my_custom_posttype')) $redirect_url = false; // change 'my_custom_posttype' , obviously
return $redirect_url;
}

One thought on “How to paginate a custom query on the single.php file

Comments are closed.