Managing two paginations in a page with timber

Rate this post

I use timber in a project, i have to do multiple queries on the search page with each a pagination.

/* search.php */
if (!isset($paged) || !$paged){
    $paged = 1;
$context = Timber::get_context(); 
$args_1 = array("post_type" => "events", "posts_per_page" => 10, "paged" => $paged);
$args_2 = array("post_type" => "news", "posts_per_page" => 10, "paged" => $paged);
$context["events"] = new \Timber\PostQuery($args_1);
$context["news"] = new \Timber\PostQuery($args_2);
Timber::render('search.twig', $context);

/* search.twig */
<!-- events -->
{% for event in events %}
    {{ event.post_title }}
{% endfor %}
{% if events.pagination.pages is not empty %}
    {% for event_page in events.pagination.pages %}
        <a href="{{ }}" class="page-numbers">{{ }}</a>
    {% endfor %}
{% endif %}
<!-- news -->
{% for new in news %}
    {{ new.post_title }}
{% endfor %}
{% if news.pagination.pages is not empty %}
    {% for new_page in news.pagination.pages %}
        <a href="{{ }}" class="page-numbers">{{ }}</a>
    {% endfor %}
{% endif %}

With this code, i get pagination for each post_type loop, The issue is that when I go to the page number of events, the other one (news) will also go to the same page number, and vice versa, normal because the url looks like this my_domain/page/2/?s=… and /page/2/ is captured by both paginations.

Sorry for my bad english, if someone has any advice I am grateful.

Thank you



I would advise you to display a specific number of posts instead for each loop and offer the user the ability to go to the post archives page.

you’re not suppose to offer a navigation on a multi-loop post index page, as the result would be just like you said, one unique permalink.

By using an archive page you would have the query result and the ability to display a post page navigation, but the permalink would be unique in each case because your’re querying 2 distinct posts or categories …

You can use get_post_type_archive_link( string $post_type ) to get the archive link.

Retrieves the permalink for a post type archive.

Leave a Reply

Your email address will not be published.

Back to top button