You can’t modify main query if you’re doing AJAX, because during AJAX requests there is no main query created at all.
On the other hand, doing this search form using AJAX isn’t the best idea – you can’t get URL for filtered list for example.
So I would remove the AJAX part and do it like so:
<form action="" method="GET" id="filter">
<?php
if( $terms = get_terms( array( 'taxonomy' => 'genre', 'orderby' => 'name' ) ) ) :
echo '<select name="genrefilter"><option value="">Select category...</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
endforeach;
echo '</select>';
endif;
?>
<?php
if( $terms = get_terms( array( 'taxonomy' => 'language', 'orderby' => 'name' ) ) ) :
echo '<select name="languagefilter"><option value="">Select category...</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
endforeach;
echo '</select>';
endif;
?>
<input type="text" name="price_min" placeholder="Min price" />
<input type="text" name="price_max" placeholder="Max price" />
<label>
<input type="radio" name="date" value="ASC" checked="checked" /> Date: Ascending
</label>
<label>
<input type="radio" name="date" value="DESC" selected="selected" /> Date: Descending
</label>
<label>
<input type="checkbox" name="featured_image" /> Only posts with featured images
</label>
<button>Apply filter</button>
<input type="hidden" name="action" value="myfilter">
<input type="hidden" name="post_type" value="<?php echo get_post_type();?>">
</form>
<div id="response">
<?php
if ( have_posts() ) :
while( have_posts() ): the_post();
echo '<h2>' . get_the_title() . '</h2>';
endwhile;
else :
echo 'No posts found';
endif;
?>
</div>
And in your functions.php
add_action( 'pre_get_posts', function ( $query ) {
if ( ! is_admin() && is_archive() && $query->is_main_query() ) {
// modify the $query according to your needs
if ( isset( $_GET['genrefilter'] ) && isset( $_GET['languagefilter'] ) ) {
$query->set( 'tax_query', array(
'relation' => 'AND',
array(
'taxonomy' => 'genre',
'field' => 'term_id',
'terms' => $_GET['genrefilter']
),
array(
'taxonomy' => 'language',
'field' => 'term_id',
'terms' => $_GET['languagefilter']
),
) );
}
}
// put your other filters here
} );
PS. You shouldn’t use <?php echo site_url() ?>/wp-admin/admin-ajax.php
. It should be <?php echo esc_attr( admin_url( 'admin-ajax.php' ) ); ?>