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