Failing at 'posts_per_page' => -1

The context: I have created a store search for a website that has listed in it's database around 1300 stores. The user enters in their postcode and is returned to them the 10 closest stores to that postcode the stores that sell the product being advertised.

The following code (php) is what I have been using for the initial search, it is looking for data in the custom taxonomy and creating an array in order to later use that data to further work out distances.

     // If lat & lng supplied, find the 10 nearest stores

    if( isset( $_GET['lat'] ) && isset( $_GET['lng'] ) ){

        $latlng = true;
        global $wp_query;

        // If the country has been set, add it to the query
        if( isset($_GET['country']) && $_GET['country'] == "true" ){

            if($_GET['ss'] == "UAE"){
                $countryname = "United Arab Emirates";
            } else {
                $countryname = $_GET['ss'];

            $args = array_merge( $wp_query->query, array( 'posts_per_page' => -1, 
                                                         'meta_key' => 'latitude', 
                                                         'store-country' => $countryname ) );   

        } else {
            $args = array_merge( $wp_query->query, array( 'posts_per_page' => -1, 
                                                         'meta_key' => 'latitude' ) );  

        //echo $wp_query->found_posts;


The code above is directly below <?php get_header(); ?>

What is happening is the header of the page is loading and ONLY the header as the rest of the page has failed to load.

I have found the problem is occuring with this line here:

$args = array_merge( $wp_query->query, array( 'posts_per_page' => -1, 

If I set posts_per_page set to 400 or anything below the code begins working the information is brought back and the page continues just fine, I am just not able to bring back ALL entries which I require. If I had less than 400 entries then there wouldn't be an issue, however I need to be able to bring back 1300 entries and that number is for ever increasing as the product is being addded to more stores.

As I understand -1 should bring back all the entries without pagenation but something is failing and I am being forced to give a specific number for posts_per_page which seems just unreasonable considering the circumstances.

It has all been working fine on my localhost, it is just occuring when the site has been live, I am being led to think that it must be something with the server settings by my hosting provider, but I wouldn't know what they would be or if that is indeed the case.


If I set posts_per_page set to 400 or anything below the code begins working

This suggests to me that it's a timing out issue. Your production server probably has certain limits that your local server did not, which is why you didn't see this problem. If you have access to php.ini you can try increasing max_execution_time or memory_limit, see if those help.

Need Your Help

Setting default values for Popup menu

python popup blender

Let's say I have this script pop-up menu with one variable (mine is more complex but simplified for clarity)

How to convert SOAP response to PHP Array?

php xml soap soap-client

I am unable to convert SOAP response to Array in php.