multiple loops with pagination on the same page

I’m searching the web for this answer for three weeks now. Tried many things but nothing seems to work as how it should work. There must be a kind of trick what probably won’t make sense to anybody what should get it working. Of course I’m talking about iterating through multiple types of your wordpress content with the option to navigate through the that of content.

The possibilities for getting a list of your content are endless and I probably tried them all, except without using wordpress functions (PHP funcs only). About each and every variation I could think of: while loop with get_posts & wp_query, foreach loop with get_posts & wp_query, external php script with the loop what works with ajax, mini loops a great plugin but didn’t fill my wishes…

So what went wrong for which variation?
I had lots of problems with the output itself. But the most common problem was the pagination. With the get_posts and wp_query while and foreach loop I was able to retrieve the right content with pagination. Only I couldn’t get the pagination itself working like it should. When clicking a pagination link it doesn’t retrieves a new list of content (the next page).

This is the basic iteration loop I use now, for my social category:

<?php
//SET THE CURRENT PAGE HERE
$socials = array(
    'paged' => $socialPaged,
    'posts_per_page' => 5,
    'category_name' => 'social'
);
$social = new WP_Query($socials);
if($social) {
    while($social->have_posts()) {
        $social->the_post();
        //DO THINGS, LOTS OF THINGS
    }
    //HERE COMES THE PAGINATION NAVIGATION
    wp_reset_postdata($social);
} else {
    echo "Sorry, there are no posts to display";
}
?>

This is what worked the best so far for defining the current page:

$socialPaged = isset($_GET['social']) ? (int) $_GET['social'] : 1;

This is what worked the best so far for my pagination navigation:

$socialPagArg = array(
    'base' => get_pagenum_link(1).'%_%',
    'format'  => '?social=%#%',
    'current' => $socialPaged,
    'total'   => $social->max_num_pages
);
echo paginate_links($socialPagArg);

Doing it this way made it possible to navigate successful in one loop. The URL will change to /?social=# if I paginate once and the content changes, but if I add an other loop and start paginating in it as well. Than the URL will show an other issue.

/?loop1=2?social=2 //paged once on loop1 and than once on social
/?loop1=2%3Fsocial%3D2?social=2 //paged once on loop1 and than twice on social

Also the content doesn’t change any more. But I prefer to have pretty URL’s, the next function did return URL’s like this /page/# but this function wont change my content list.

next_posts_link('Older',$social->max_num_pages);

How it should work in the url:

/page/cat1/2 //paged once in loop cat1
/page/cat1/2/cat2/2 //started paging in cat2 as well
/page/cat1/3/cat2/2 //paged once more in loop cat1
/page/cat1/3/cat2/3 //paged once more in loop cat2

But this is also one big mess, is the use of sessions not better?

0

Leave a Comment