Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this AJAX call to fill a dropdown:

function get_select_companies()
{
    var t_id = $('select#select-product_types').val();
    if( t_id == -1 )
    {
       $('#ajax-loader').hide();
       $('#select-manufacturers').hide();
    }
    else
    {
    $('#ajax-loader').show();
        $('#select-manufacturers').hide();
        $('#select-manufacturers option').remove();

    jQuery.ajax(
        {
            url:     '/wp-admin/admin-ajax.php',
            type:    'POST', 
        data:    (
            { 
            action:         'px_get_select_companies', 
            type_id:        t_id 
            }),
                dataType: 'JSON',
        success: function(data)
                {
            $('#select-manufacturers').append(data);
            $('#ajax-loader').hide();
            $('#select-manufacturers').show();       
        }
        });
    }
}

I then have this php function on my functions.php file

function px_get_select_companies()
{
$args = array(
    'post_type'     => 'products',
    'posts_per_page' => -1,
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'product_types',
            'field' => 'id',
            'terms' => array( $_POST['type_id'] )
        ),
    )
);

$query = new WP_Query( $args );

while( $query->have_posts() ) : $query->the_post();
    if( $query->post->ID != 1 ) :
        $terms[] = get_the_terms( $query->post->ID, 'manufacturers' );
    endif;
endwhile;

foreach( $terms as $term ) :
    foreach( $term as $ID ) :
        $term_IDs[] = $ID->term_id;
    endforeach;
endforeach;

$term_IDs = array_unique( $term_IDs );

$output = '<option value="0" selected="selected">--- Επέλεξε Εταιρεία ---</option>';

foreach( $term_IDs as $ID ) :
    $term = get_term_by( 'id', $ID, 'manufacturers' );
    $output .= '<option value="' . $term->term_id . '">' . $term->name . '</option>';
endforeach;

wp_reset_postdata();

$output=json_encode($output);

if(is_array($output)){
    print_r($output);   
}
else
{
    echo $output;
}
die;
}

The problem is that I get an endless spinner as it's not returning any results. If i change posts_per_page to 100 or less, it works as expected. Anyone have an idea as to why it's doing this?

Thanks

share|improve this question
1  
How long does the query take to execute, and how long is the php max_execution_time set to? –  Otto Feb 27 '13 at 4:22
    
I looked at the php.ini and max_execution_time = 300 –  phoat Feb 27 '13 at 18:20
add comment

1 Answer

As otto implied in his comment, your code execution time is probably greater then the max php execution set in max_execution_time configuration directive in your php.ini, which is usually 30 seconds.

You might be able to change that directive in php.ini or .htaccess, but it will only create a new problem as the user will have to wait a long time until the data will load. 30 seonds is usually unaccepted response time for anything.

In any case even from the UX standpoint 100 items in a dropdown is much to much. You probably need to rethink your GUI.

share|improve this answer
    
The dropdown doesn't return 100 items, as you can see from the code, I use array_unique to only return the manufacturer once in the list. The way it works now is the user chooses a product category, lets say notebooks, and the manufacturer dropdown goes through all the posts and returns only the products in the notebook taxonomy. Then it groups the posts by manufacturer so that the user can choose a company. When a company is choosen, a third dropdown is populated with products by that company. –  phoat Feb 27 '13 at 16:39
    
I think that get_terms('manufacturers') will do exactly what you want to do, if you want only the list of manufacturers. go for the codex to see full documentation of the arguments it can get –  Mark Kaplun Feb 27 '13 at 17:04
    
Yes it does, and I guess I should have explained it better. I want manufacturers that have a product in the category chosen. So the list depends on what is chosen in the product category dropdown. I'm filtering results essentially. Choose a product category, then choose a company that has a product in that category, then choose a product and finally submit. Its for a feature comparison website comparing tech specs of various hardware. –  phoat Feb 27 '13 at 18:16
    
Thank you btw for taking the time to help me :) –  phoat Feb 27 '13 at 18:16
    
I looked at the php.ini and max_execution_time = 300 –  phoat Feb 27 '13 at 18:19
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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