E
E
Egor Dovydenko2021-02-15 11:15:10
WordPress
Egor Dovydenko, 2021-02-15 11:15:10

how to do ajax filter in wordpress

I have a wordpress store with woocommerce.
The task is to sort without reloading the page. The theme was not developed by me.
There is a file with custom php. It has a filter. Help to make changes for ajax loading of goods.
Here is the filter code:

function go_filter(){

  $args = array();   

  // фильтруем пост по таксономии с иерархией (как у категорий), причем пост должен принадлежать двум терминам таксономии одновременно

  global $wp_query; // нужно заглобалить текущую выборку постов

  // сортировка

  if( isset( $_GET['prod_typeorder']) ){

    if ( $_GET['prod_typeorder'] == 'price') {

      $args  = array( 

        'meta_key' => '_price',

        'orderby' => 'meta_value_num',

        'order' => 'ASC',

      ); 

    } 

    elseif($_GET['prod_typeorder'] == 'price-desc'){

      $args  = array( 

        'meta_key' => '_price',

        'orderby' => 'meta_value_num',

        'order' => 'DESC',

      ); 

    }

    else {

      $args  = array(

        'orderby' => $_GET['prod_typeorder'], 

      ); 

    } 

  }	

  // фильтр по категориям

  if( isset( $_GET['categoryfilter']) ){

    $args['tax_query'] = array(

      array(

        'taxonomy' => 'product_cat',

        'field' => 'id',

        'terms' => $_GET['categoryfilter']

      )

    );

  }	

  // фильтр по цене

  if( isset( $_GET['my_range'] )   ) {  

    $min_max = explode(";", $_GET['my_range']);

    $args['meta_query'][] = [

      'key'     => '_price',

      'value'   => [(int)$min_max[0], (int)$min_max[1]],

      'type' => 'numeric', 

      'compare' => 'BETWEEN',

    ];  

  }

  query_posts(array_merge($args,$wp_query->query));

  ?>
  <?php 

}

add_action('wp_ajax_myfilter', 'true_filter_function'); 

add_action('wp_ajax_nopriv_myfilter', 'true_filter_function');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vadim Tatarskikh, 2021-02-25
@slad777

the filter will have to be rewritten or use a plugin

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question