I have a search form, a map and results section.

  • Search form is submitted with different parameters
  • Map shows ALL the results that match the parameters
  • Results section shows first 15 results that match the parameters (ordered by something)

I currently have 2 different queries with only one difference:

  • posts_per_page for map is -1
  • posts_per_page for results is 15

  1. Is there a way / hack to get all the posts but show only 15 in results section?
  2. Can I somehow show all the posts (or X amount less) without using posts_per_page -1 and without knowing the count? This if for WP_Query offset argument that can't be used if posts_per_page is -1.

I know that this might seem like overcomlicating things but 2 basically same queries at same time with huge amount of posts (not now but it will be) and with a lot of arguments? Seems like a huge waste.

  • Why does the map show all results when the user has already searched for their desired results? Why wouldn't you just have the map show the results that they want? It's difficult to tell what you're trying to achieve but maybe overflow: hidden will help if you're referring to presentation?
    – Howdy_McGee
    Dec 28, 2015 at 19:28
  • I think you misunderstood my situation. Map shows all the results that match while results section shows only first 15 of these results that match because it wouldn't be wise to load 500 different post-items with thumbnails, excerpts and few terms.
    – N00b
    Dec 28, 2015 at 19:30
  • You could easily bust the query into two queries. While getting the same result pretty much the most taxing is always going to be the posts_per_page => -1 and the 15 would be light in comparison. Another option would be use a counter and only show the first 15 results then run ajax to pull more results with an offset of 15. You would still need to rerun the query in ajax but it would be just the same at a ppp of 15. Finally, a 3rd option is to use CSS to hide / show your results which is faking pagination but still works. I'm not sure this is a question that can have a concrete answer.
    – Howdy_McGee
    Dec 28, 2015 at 19:36
  • Im currently using option 2 - I have two different queries, map query has posts_per_page -1, results query has posts_per_page 15 and I use ajax with offset to load 15 more queries with every Show more button click. I was just thinking that while queries are exactly the same with only difference being posts_per_page, I could get away with only 1 query (except ajax still runs its own when Show more is clicked) because my query could easily have 6 tax_query and like 14 meta_query arguments if users use all the inputs and it's pretty nasty to run it two times.
    – N00b
    Dec 28, 2015 at 19:45
  • You can just show the first 15 results of a query- count to 15 and break out of the loop. Loops can be run multiple times on the same query object. You'll need to think this through though- if that huge amount of posts keeps growing, you'll eventually run out of memory and your query will produce a fatal error. You should figure out where you need to cap that now rather than when it stops working.
    – Milo
    Dec 28, 2015 at 19:52

1 Answer 1


Here's some options:

Keep The AJAX

You drop out your posts_per_page => 15 query and keep the -1 query. Then after your map loop ( or before ) you can limit the amount of items showing using a counter:

if( $map->have_posts() ) : $counter = 1;
    while( $map->have_posts() && $counter <= 15 ) :

You could then use $map->rewind_posts() respectively. Now the AJAX portion you would requery the results but since it's not happening during page-load you'll see a speed increase. You then just offset => 15 and posts_per_page => 15 so it's a faster query, return results and append or replace what you currently have in the results section.

Simple Hide / Show

In this scenario you would want to use the rewind_posts() mentioned earlier. Pretty much you would loop through all the results putting them in <div>'s of 15 ( I suggest using modulus to test this. You can then use jQuery to process when the user clicks "next" to show the next div which has a set of 15. Since you're only using 1 query the page speed will be based on the looping which will turn out to be fairly quick.

  • If that changes anything, form submission and getting search results is also via ajax. Only the first query is run in normal way if page is initially loaded (without form inputs which basically means all results). Your first suggestion seems like a way to go because I don't even want to think about loading times when it would have to load few hundred or more post-items each with 20-25 dom elements. But thank you, this might be what I was looking for.
    – N00b
    Dec 28, 2015 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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