Halloween Sale 15% off Ends in See the plans
PHP

"View All" Link on Paginated Archives

Adds a "View all 42 posts on one page" link under paginated category, tag and other archives that are not too large, for readers who prefer to scroll or search the page.

Don’t have WPIDE yet? Install it on your site

Code

// Show the "View all" option only when the archive has no more than this many posts.
$wpide_view_all_max = 150;
// Link text; %d is the number of posts.
$wpide_view_all_label = 'View all %d posts on one page';

$wpide_view_all_on = function () {
    return isset( $_GET['view'] ) && 'all' === sanitize_key( wp_unslash( $_GET['view'] ) );
};

add_action( 'pre_get_posts', function ( $query ) use ( $wpide_view_all_max, $wpide_view_all_on ) {
    if ( ! $wpide_view_all_on() || is_admin() || ! $query->is_main_query() || ! $query->is_archive() ) {
        return;
    }

    $query->set( 'posts_per_page', absint( $wpide_view_all_max ) );
    $query->set( 'paged', 1 );
} );

// The single-page copy is for readers; search engines keep the normal paginated pages.
add_filter( 'wp_robots', function ( $robots ) use ( $wpide_view_all_on ) {
    if ( $wpide_view_all_on() ) {
        $robots['noindex'] = true;
        $robots['follow']  = true;
    }

    return $robots;
} );

$wpide_view_all_link = function () use ( $wpide_view_all_max, $wpide_view_all_label ) {
    global $wp_query;

    if ( ! is_archive() || isset( $_GET['view'] ) || $wp_query->max_num_pages < 2 || $wp_query->found_posts > $wpide_view_all_max ) {
        return '';
    }

    $url = add_query_arg( 'view', 'all', get_pagenum_link( 1, false ) );

    return '<p class="wpide-view-all" style="text-align:center;margin:1.5em 0;"><a href="' . esc_url( $url ) . '">' . esc_html( sprintf( $wpide_view_all_label, (int) $wp_query->found_posts ) ) . '</a></p>';
};

// Classic themes: after the last post.
add_action( 'loop_end', function ( $query ) use ( $wpide_view_all_link ) {
    static $done = false;

    if ( $done || wp_is_block_theme() || ! $query->is_main_query() ) {
        return;
    }

    $done = true;
    echo $wpide_view_all_link(); // phpcs:ignore WordPress.Security.EscapeOutput -- escaped when built.
} );

// Block themes: after the page numbers.
add_filter( 'render_block_core/query-pagination', function ( $content ) use ( $wpide_view_all_link ) {
    static $done = false;

    if ( $done || '' === trim( $content ) ) {
        return $content;
    }

    $done = true;

    return $content . $wpide_view_all_link();
} );

Notes

Only shown for archives up to 150 posts; the single-page version is marked noindex.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.