Back to Blog

Multiple WP_Query Loops in One Page with Pagination

Loop through multiple custom post types having pagination in one page

Jan 2, 2023 Updated: Jan 2, 2023

template-custom-page.php
<?php

/**
 * Template Name: Custom Page
 */

get_header();

$paged1 = isset($_GET['paged1']) ? (int) $_GET['paged1'] : 1;
$paged2 = isset($_GET['paged2']) ? (int) $_GET['paged2'] : 1;
?>

<section>
    <h2>Loop 1</h2>
    <?php
        $query1 = new WP_Query(array(
            'post_type' => 'page',
            'post_status' => 'publish',
            'paged' => $paged1,
            'posts_per_page' => 2,
            'posts_per_archive_page' => 2,
            'ignore_sticky_posts' => true,
            'suppress_filters' => true,
            'orderby' => 'modified',
            'order' => 'DESC',
        ));
        while ($query1->have_posts()) {
            $query1->the_post();
            echo '<h3>' . get_the_ID() . ': ' . get_the_title() . '</h3>';
        }
        echo paginate_links(array(
            'format' => '?paged1=%#%',
            'current' => $paged1,
            'total' => $query1->max_num_pages,
            'add_args' => array('paged2' => $paged2),
        ));
    ?>
</section>

<section>
    <h2>Loop 2</h2>
    <?php
        $query2 = new WP_Query(array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'paged' => $paged2,
            'posts_per_page' => 2,
            'posts_per_archive_page' => 2,
            'ignore_sticky_posts' => true,
            'suppress_filters' => true,
            'orderby' => 'modified',
            'order' => 'DESC',
        ));
        while ($query2->have_posts()) {
            $query2->the_post();
            echo '<h3>' . get_the_ID() . ': ' . get_the_title() . '</h3>';
        }
        echo paginate_links(array(
            'format' => '?paged2=%#%',
            'current' => $paged2,
            'total' => $query2->max_num_pages,
            'add_args' => array('paged1' => $paged1),
        ));
    ?>
</section>

<?php
get_footer();
Contact

Got A Question For Me?

Feel free to ask anything directly on call or fill the form and I will contact back within few hours.