1

I am trying to build a classic archive page with 10 posts per page. But with a list of all the posts of this archive to be displayed on top of the classic loop.

(The paginated posts would display feature image, excerpt... while the list of all post would retain only the permalink)

I keep on thinking I could use the global loop already processed and hook just before pagination to get data and then let it paginate.

But I can't find how so far.

Another solution would be to run 2 loops on the page, one with pagination and one without, but the first method is probably better and possible, I think....

Did you guys ever came across similar issue? Any suggestion to this problem?

  • I don't clearly understand your question. What you want to do is to create like an index with just the post names of a particular archive, say for the month May. if there is hundred posts, it will show 100 post titles, no pagination. Directly after this index list if I can call it this, you need the actual posts, 10 per page with pagination – Pieter Goosen May 3 '14 at 8:29
  • Yes Pieter that's it! so far my research lead to an extra query right after the global query. It will query the same post type as the archive butt this time with posts_per_page=-1. I would have loved to avoid this second query – Daco May 3 '14 at 9:13
  • Thanks, apparently it's the way to go, I think the posts_per_page works same as a the LIMIT in standard sql, so once it has ran with LIMIT the query is limited :-). I thought since $found_posts return all post despite pagination feature, I would found a way around it. So far no luck... – Daco May 3 '14 at 9:21
  • 2
    Post the code that you have, working or not, and just add a description where you are stuck and what your code does/or not – Pieter Goosen May 3 '14 at 9:34
2

If you want to accomplish it in a single query, hook pre_get_posts to query for all posts:

function wpd_archive_all_posts( $query ){
    if( $query->is_post_type_archive( 'attractions' ) && $query->is_main_query() )
        $query->set( 'posts_per_page', -1 );
}
add_action( 'pre_get_posts', 'wpd_archive_all_posts' );

Then paginate those results manually in the template:

if( have_posts() ){

    $posts_per_page = 10;
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $start = ( ( $paged - 1 ) * $posts_per_page ) - 1;
    $end = ( $paged * $posts_per_page ) - 1;
    $wp_query->current_post = $start;

    while( have_posts() ){
        the_post();

        // output post data here

        if( $end == $wp_query->current_post )
            break;

    }

}

Then rewind and output all of them:

$wp_query->rewind_posts();
while( have_posts() ){
    the_post();
    the_title();
}

The caveat here is that the first loop may not call the loop_end action, since you could be ducking out of the loop before hitting the last post. In practice this may or may not have side effects, depending on whether or not you have a plugin hooking that action.

|improve this answer|||||
  • response accepted, Pieter Goosen and Milo: you guys helped me a lot on this! Thanks a lot! – Daco May 5 '14 at 8:47
0

Pulled directly from wordpresses codex:

// main loop
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>


// rewind
<?php do something with query_posts() here to modify the loop here. ?>
<?php rewind_posts(); ?>

// new loop
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>

Note, the rewind_posts() does just that, it rewinds the post loop to the beginning, allowing you to loop through it a second time.

However, if what you're wanting is 2 somewhat different lists, ie: the first loop is a list of 100 posts, and the second is a paged list of only 10 posts per page, you can use query_posts() to alter the query before you run it a second time. It's generally not advisable to use query_posts(), as it alters the main loop, but in situations like this, it will be your best bet.

|improve this answer|||||
  • 1
    I still think that there should be a better method than to use query_posts to run two diffirent loops on a normal archive page. True, query_posts solves the problem, I'm able to do exactly what the asker needs. I would really like someone to come up with something better than query_posts. BTW, your code only works if it is a straight forward no special query. – Pieter Goosen May 3 '14 at 19:00
  • Well as I'm unaware of exactly what OP is running query wise, I omitted it. However, I did add where you'd want to run the query_posts(). Being that essentially OP's question in fact requires 2 seperate queries, I don't believe there's a way to avoid running a second query. – Hybrid Web Dev May 3 '14 at 19:18
  • Thanks for this, I did ran a second query in the end. But would have loved to avoid it! maybe someone will have a better idea... – Daco May 3 '14 at 20:07
  • @Daco please post your code that solved your problem as an answer. Would be nice to see how you've done it. You might also get a few suggestions to better your code. – Pieter Goosen May 4 '14 at 3:22
  • 1
    Glad you got it figured out Daco. If my answer helped, could you accept it? Also as Pieter said, it'd be great if you edited your OP to include the code, so that others may benefit. – Hybrid Web Dev May 4 '14 at 9:01
0

Well I'm not sure it's the best solution, while monitoring it does less queries than a standard archive page? maybe because I didn't call the meta like author and date... If anyone have a real anwer please let me know Cheers

//first the main loop paginated:
if ( have_posts() ) :
while ( have_posts() ) : the_post();
    //get permalink, title,custom metas and feature image
else :
    get_template_part( 'content', 'none' );
endif;
//then I run the second loop for all posts of the Custom Post Type
$args = array(
        'post_type'  => 'attractions',
        'post_status' =>    'publish',
        'posts_per_page'=>-1
    );
$wp_query = new WP_Query($args);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
    //get datas of all posts
endwhile; wp_reset_postdata(); 
|improve this answer|||||
  • Your question was fague. The fact that you are using a CPT completely change the whole ball game. Should have mentioned that in your question, would have been much easier to solve. – Pieter Goosen May 4 '14 at 17:32
  • Sorry Pieter I didn't imageine it would make it that much easier! :-), do you think Milo's solution is the best? – Daco May 5 '14 at 8:07
  • I would definitely give it a try. I personally think it is a great solution – Pieter Goosen May 5 '14 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.