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 )
        $('#select-manufacturers option').remove();

            url:     '/wp-admin/admin-ajax.php',
            type:    'POST', 
        data:    (
            action:         'px_get_select_companies', 
            type_id:        t_id 
                dataType: 'JSON',
        success: function(data)

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',
            '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' );

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

$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>';



    echo $output;

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?


  • 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

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.

  • 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
  • I looked at the php.ini and max_execution_time = 300 – phoat Feb 27 '13 at 18:19
  • In that case you need to execute a more specific query that gets the terms that have a post in the category. Unfortunately my SQL mojo isn't great. Maybe you should pop up at the "stoppress" chat where the editors usually hangout and ask them if you should open a new question about that. – Mark Kaplun Feb 27 '13 at 18:55

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.