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
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.