Thursday, July 21, 2022
HomeWordPress Developmenttheme improvement - Pagination do not work with energetic filters

theme improvement – Pagination do not work with energetic filters


I am make a customized theme para WordPress and all works positive besides the pagination when the filters are energetic. The console return 400 error and the navigation present /wp-admin/admin-ajax.php?paged=2

The mission’s repository is public in https://github.com/madebyjona/tuplan

Thanks for assist.

<?php
    
  operate cpt_pagination($pages="", $vary = 4)
  {
      $showitems = ($vary * 2)+1;
      world $paged;
      if(empty($paged)) $paged = 1;
      if($pages == '')
      {
          world $wp_query;
          $pages = $wp_query->max_num_pages;
          if(!$pages)
          {
              $pages = 1;
          }
      }
      if(1 != $pages)
      {
          echo "<nav id='inmuebles__pagination'>  <ul class="pagination"> <span>Página ".$paged." de ".$pages."</span>";
          if($paged > 2 && $paged > $vary+1 && $showitems < $pages) echo "<a href="https://wordpress.stackexchange.com/questions/407864/".get_pagenum_link(1)."">&laquo; Primera</a>";
          if($paged > 1 && $showitems < $pages) echo "<a href="".get_pagenum_link($paged - 1)."">&lsaquo; Anterior</a>";
          for ($i=1; $i <= $pages; $i++)
          {
              if (1 != $pages &&( !($i >= $paged+$vary+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
              {
                  echo ($paged == $i)? "<li class="page-item energetic"><a category="page-link">".$i."</a></li>":"<li class="page-item"> <a href="".get_pagenum_link($i)."" class="page-link">".$i."</a></li>";
              }
          }
          if ($paged < $pages && $showitems < $pages) echo " <li class="page-item"><a category="page-link" href="".get_pagenum_link($paged + 1)."">i class="flaticon flaticon-back"></i></a></li>";
          if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo " <li class="page-item"><a category="page-link" href="".get_pagenum_link($pages).""><i class="flaticon flaticon-arrow"></i></a></li>";
          echo "</ul></nav>n";
      }


}


?>

pagination.php

<?php

operate filter_products()
{
  $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
  $tipos = $_POST['tipos'];
  $propiedad = $_POST['propiedad'];
  $zonas = $_POST['zonas'];
  $price_min = $_POST['price_min'];
  $price_max = $_POST['price_max'];

  $args = [
    'post_type' => 'inmueble',
    'posts_per_page' => 3,
    'post_status'  => 'publish',
    'orderby' => 'date',
    'order' => 'DESC',
    'paged' => $paged,
  ];

  if (depend($tipos) > 1) {
    $args['tax_query'][] = [
      'taxonomy'      => 'tipos',
      'field'          => 'slug',
      'terms'         => $tipos,
      'operator'      => 'IN',
    ];
  }

  if (depend($propiedad) > 1) {
    $args['tax_query'][] = [
      'taxonomy'      => 'propiedad',
      'field'          => 'slug',
      'terms'         => $propiedad,
      'operator'      => 'IN',
    ];
  }

  if (depend($zonas) > 1) {
    $args['tax_query'][] = [
      'taxonomy'      => 'zonas',
      'field'          => 'slug',
      'terms'         => $zonas,
      'operator'      => 'IN',
    ];
  }

  if (isset($price_min) && $price_min || isset($price_max) && $price_max)
    $args['meta_query'] = array('relation' => 'AND'); 

  if (isset($price_min) && $price_min && isset($price_max) && $price_max) {
    $args['meta_query'][] = array(
      'key' => '_tp_price',
      'worth' => array($price_min, $price_max),
      'kind' => 'numeric',
      'evaluate' => 'between',
    );
  } else {
    if (isset($price_min) && $price_min)
      $args['meta_query'][] = array(
        'key' => '_tp_price',
        'worth' => $price_min,
        'kind' => 'numeric',
        'evaluate' => '>'
      );

    if (isset($price_max) && $price_max)
      $args['meta_query'][] = array(
        'key' => '_tp_price',
        'worth' => $price_max,
        'kind' => 'numeric',
        'evaluate' => '<'
      );
  }

  $ajaxproducts = new WP_Query($args);
  $response = "";
  $counter = "";

  if ($ajaxproducts->have_posts()) {
    ob_start();
    whereas ($ajaxproducts->have_posts()) : $ajaxproducts->the_post();
      $response .=  get_template_part('template-parts/content material/content material', 'list-item');
    endwhile;
    cpt_pagination($ajaxproducts->max_num_pages);
    $output = ob_get_contents();
    ob_end_clean();
  } else {
    ob_start();
    $response .= get_template_part('template-parts/content material/content material', 'no-items');
    $output = ob_get_contents();
    ob_end_clean();
  }

  $consequence = [
    'total' => $counter,
    'html' => $output,
  ];

  echo json_encode($consequence);

  wp_reset_postdata();
  exit;
}


add_action('wp_ajax_filter_products', 'filter_products');
add_action('wp_ajax_nopriv_filter_products', 'filter_products');

filter-function.php

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments