How do I toggle pagination on/off in search results and category listings via a link?

On this website, the user wants to be able to switch off pagination, so as to be able to send the entire results of a search or category query to print as one big document. Seems obvious, so I was surprised to find no plugin… now maybe I know why.

I’ve been wrestling with this for a while, and I fear my understanding of WordPress just isn’t up to it… what I’ve been trying is:

  1. adding a query string to the URL: ?onepageprint=1 (works)
  2. registering a ‘disable pagination’ function (works)
  3. testing for the query string and conditionally calling the function (doesn’t work).

The problem I think I have is that pagination is set up too early in the WordPress process, before it gets to a place where I can get the query string out of the URL and test for it (without hacking the core!)

So two questions and a thought:

  1. am I going about this the right way?
  2. is my sense of the problem correct and, if so, is there a workaround?
    and if this really isn’t going to work (latest attempt below)…
  3. might it be possible to do this with redirect_canonical (which I admit don’t really understand at all!)

I’ve messed with endless variations of this – here’s the latest attempt (though there may be some duplication). Setting the query string and getting it out of the URL seems to work fine, and the function for disabling pagination works too… but when it’s in place it always works (or never works), and I can’t find any way of conditionally switching it on and off:

In functions.php:

/* register custom query var (from URL) to toggle pagination */
function jr_add_query_vars_filter( $displayvars ){
    $displayvars[] = "onepageprint";
    return $displayvars;
}
add_filter( 'query_vars', 'jr_add_query_vars_filter' );
get_query_var( 'onepageprint', 0 );

/* disable pagination conditionally on custom query var setting (from URL query string) 
 * NOT WORKING YET */
add_Action('init', 'remove_pagination');

function remove_pagination() {
if ( ! $onepageprint == 1 )
    return; // stop here.
    /* Disable content pagination, based on
     * https://wp-types.com/forums/topic/remove-pagination-from-archive-view/ */
    add_action('parse_query', 'no_nopaging');
    function no_nopaging($query) {
    $query->set('nopaging', 1);
    }
}

In page templates (one of many attempts) – I guess this, or something like it, needs to go in both search.php and archive.php:

// call no-pagination function - DIDN'T WORK
if ( $onepageprint == 1 ) {
no_nopaging($query);
}

I hope this makes some sense, and do let me know if there’s more you need about my config to answer it. Unfortunately I can’t post links to the actual site as it’s a private research site, and everything on it is confidential!

Even some pointers to a better way of doing this would be great. Many thanks.

[Edit: title improved just after posting!]

Solutions Collecting From Web of "How do I toggle pagination on/off in search results and category listings via a link?"

I think it should work by hooking into pre_get_posts:

function wpse_254661_remove_pagination( $query ) {
    if ( $query->is_main_query() &&  get_query_var( 'onepageprint', 0 ) ) {
        $query->query_vars['nopaging'] = 1;
        $query->query_vars['posts_per_page'] = -1;
    }
}
add_action( 'pre_get_posts', 'wpse_254661_remove_pagination' );

Like this you can modify your query right before it gets fetched and the query object gets passed by the hook right into the function. There is also a check to only affect the main query.