D
D
DenMoo2021-12-19 21:35:48
AJAX
DenMoo, 2021-12-19 21:35:48

Why is Wp Ajax resetting the language?

There is a custom post type archive page. The main language of the site is German, the filter works on it (except for pagination). The additional language of the site is English, if you switch the language and filter the records, then it’s also out of order, but as soon as I refresh the page, records in German get into the ajax request. What could be the problem?

The form

<form class="filter_form" action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter_form" >
          <fieldset>
            <legend class="filter_title"><?php echo __('Filter','leihmutterschaftwelt')?></legend>
            <span class="filter_subtitle"><?php echo __('Land','leihmutterschaftwelt')?>:</span>
            <div class="check_box">
                <label>
                  <input  type="checkbox" name="land[]" value="ukraine">
                  <span>Ukraine</span>
                </label>
                                <label>
                  <input  type="checkbox" name="land[]" value="usa">
                  <span>USA</span>
                </label>
            </div>
<input class="check" type="hidden" name="action" value="doctor_filter">
            <input type="submit" id ="submit" name="submit" value="<?php echo __('Anwenden','leihmutterschaftwelt')?>" />
          </fieldset>
        </form>


JS
const filter_form = document.getElementById("filter_form");
filter_form && filter_form.addEventListener('submit', (event) => {
 startfilter();
 event.preventDefault();
});

function startfilter(){
  var action = filter_form.getAttribute('action');
  var checked = document.querySelectorAll("input[type=checkbox]:checked");
    var formData = new FormData(); 
        for(var i=0; i<checked.length; i++)
    {
        formData.append(checked[i].name, checked[i].value);
    }
    const data = formData.toString();
    var xmlHttp = new XMLHttpRequest();
        xmlHttp.onreadystatechange = function()
        {
            if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
            {
                 document.getElementById('main_filter_content').innerHTML = xmlHttp.responseText;
            }
        };
        xmlHttp.open("post", action); 
        xmlHttp.send(formData); 
  }


Handler

add_action('wp_ajax_doctor_filter', 'leihmutterschaftwelt_filter_doctor_function'); // wp_ajax_{ACTION HERE}
add_action('wp_ajax_nopriv_doctor_filter', 'leihmutterschaftwelt_filter_doctor_function');

function leihmutterschaftwelt_filter_doctor_function(){
  global $wp_query;
  $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

  $args = array(
    'post_type' => 'arzt',
    'post_status'=>'publish',
    'paged' => $paged
  );
  if( isset( $_POST['land'] ) && '' != $_POST[ 'land' ] ) {
    $args['meta_query'][] = array(
      'key' => '_land',
      'value' => $_POST['land'],
      'compare' => 'IN' 
    );
  }

  $wp_query = new WP_Query( $args );	?> 
  <div class="list_container"> 
  <?php
  if( $wp_query->have_posts() ) :
    while( $wp_query->have_posts() ): $wp_query->the_post();
      $land = carbon_get_post_meta (get_the_ID(), 'land');
      $position = carbon_get_post_meta (get_the_ID(), 'position');
    ?>
  <div class="panel_item">
    <div class="panel_img">
        <img decoding="async" src="<?php the_post_thumbnail_url('full'); ?>" width="800" height="600" alt="<?php the_title(); ?>">
    </div>
    <div class="panel_text">
      <div class="title"><?php the_title();?></div>
      <div class="position"><?php echo __('Position','leihmutterschaftwelt')?>:<a href="#"><?php echo $position?></a></div>
      <div class="country"><?php echo __('Land','leihmutterschaftwelt')?>:<a href="#"><?php echo $land ?></a></div>
    </div>
    <div class="panel_button">
      <a href="<?php echo get_permalink(); ?>" class="main_button"><?php echo __('Mehr Details','leihmutterschaftwelt')?></a>
    </div>
  </div>
  <?php endwhile; 
    wp_reset_postdata();
  ?>
  </div>
  <div class="navigation">
    <?php						
      the_posts_pagination( array(
        'prev_text'          => '',
        'next_text'          => '',
        'before_page_number' => '',
      ) );
    ?>

    <div class="clearfix"></div>
  </div>
  <?php else :
    echo 'No posts found';
  endif;
  die();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aricus, 2021-12-20
@Aricus

if you switch language and filter entries

So this data is not stored. You can save data when refreshing the page either in get-parameters or in sessions / cookies (which is used on the site). Next, you need to get them from there and make them the default value for these switches.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question