• Resolved JimFitzsimons

    (@jimfitzsimons)


    Oops, I put this in the “plugins and hacks” bit instead of just the Hacks forum. Hopefully it’s more appropriate here.

    Basically I want to output a list of all posts returned by a search query (using the standard WordPress search interface) in the site sidebar. Because my pages are set up to only show 5 posts, when I re-output the loop in the sidebar, I only get a list of the first 5 posts from the query (changing to the next 5 when I click the “older posts” navigation.

    So I want to output the loop once as standard (5 per page) and then re-output the same loop/query but as a list of all post titles.

    I’ve tried a fair few different things, but nothing has worked yet. I’m sure there must be a way of taking an existing query and simply changing the “posts_per_page” parameter. But how?

Viewing 1 replies (of 1 total)
  • Thread Starter JimFitzsimons

    (@jimfitzsimons)

    I found the answer myself by fitting together a couple of solutions to slightly different problems… So just in case anyone else needs to do the same thing:

    First output the main loop as standard in your search.php (or index.php or wherever) template. Then to list all of the post titles in your sidebar, you need to first re-grab the query but with posts_per_page set to -1:
    <?php query_posts(array_merge(array('posts_per_page' => -1), $wp_query->query)); ?>

    Then output the list of titles:

    <?php if (have_posts()) : ?>
    	<ul>
    	<?php while (have_posts()) : the_post(); ?>
    	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    	<?php endwhile; ?>
    	</ul>
    <?php else : ?>
    	<p>No posts match that term. Feel free to search again</p>
    <?php endif; ?>

    Then finally reset the query to 5 posts_per_page (or however many you’re using) to maintain pagination in the main content area:
    <?php query_posts(array_merge(array('posts_per_page' => 5), $wp_query->query)); ?>

    And that should work!

Viewing 1 replies (of 1 total)
  • The topic ‘output search results twice?’ is closed to new replies.