Ecommerce Shopify WordPress Discussion

Pagination ajax not working, return a error 400 bad request

I'm trying to do ajax pagination in wordpress, but I get a 400 bad request error without further information when I try to use it. all the php code is in the functions.php file. This is my code: PHP loading my javascript: function enqueue_pagination_script() { $pages = [ 'inversion-y-gestion-presupuestaria', 'acuerdos-de-entrada-gestionada', ]; if (is_page($pages)) { wp_enqueue_script( 'pagination-script', get_template_directory_uri() . '/js/ajax-querys.js', ['jquery'], '1.0.0', true ); $variable_to_js = [ 'ajax_url' => admin_url('admin-ajax.php'), ]; wp_localize_script('pagination-script', 'ajax_object', $variable_to_js); } } The first function is to make pagination by buttons, which is the one that brings the pagination links: <?php add_action('wp_ajax_nopriv_filter_ajax_multimedia', 'filter_ajax_multimedia'); add_action('wp_ajax_filter_ajax_multimedia', 'filter_ajax_multimedia'); function filter_ajax_multimedia() { $paged = get_query_var('paged') ? get_query_var('paged') : ''; $categoria = isset($_POST['categoria']) ? sanitize_text_field($_POST['categoria']) : ''; if ($categoria === 'todos') { $query_args = [ 'post_type' => 'recursos_multimedia', 'post_status' => 'publish', 'order' => 'ASC', 'orderby' => 'date', 'posts_per_page' => '12', 'offset' => '0', 'paged' => '1', // 'page' => '2', ]; // var_dump('todos'); // $wp_query = new WP_Query($query_args); } else { $query_args = [ 'post_type' => 'recursos_multimedia', // 'posts_per_page' => $countPPP, 'post_status' => 'publish', 'order' => 'ASC', 'orderby' => 'date', 'posts_per_page' => '12', 'offset' => '0', 'paged' => '1', // 'orderby' => 'date', 'tax_query' => [ [ 'taxonomy' => 'categoria', 'field' => 'slug', 'terms' => $categoria, ], ], ]; } $wp_query = new WP_Query($query_args); if ($wp_query->have_posts()): ob_start(); $categorias = []; while ($wp_query->have_posts()): $wp_query->the_post(); $term = get_the_terms(get_the_ID(), 'categoria'); foreach ($term as $term) { $categorias[] = $term->slug; } ?> <div class="show-on-scroll item <?php echo $term->slug; ?>"> <a href="<?php the_permalink(); ?>"> <span class="videoMask"> <img src="<?php the_post_thumbnail_url('medium'); ?>" alt=""> </span> <div class="categoriaRecursoMultimedia <?php echo $term->slug; ?>"> <?php echo $term->name; ?> </div> <p><?php echo wp_trim_words(the_title(), 10, '...'); ?></p> </a> </div> <?php endwhile; $current_category = isset($term->slug) ? $term->slug : ''; // Obtén los enlaces de paginación $pagination = paginate_links([ 'total' => $wp_query->max_num_pages, 'current' => max(1, $paged), 'format' => '?paged=%#%', 'show_all' => false, 'type' => 'array', 'prev_next' => true, // 'end_size' => 4, 'prev_text' => __( '<img src="' . get_template_directory_uri() . '/images/icons8-atras.svg" />' ), 'next_text' => __( '<img src="' . get_template_directory_uri() . '/images/icons8-adelante.svg" />' ), ]); $response = [ 'type' => 'success', 'html' => ob_get_clean(), 'page' => $paged, 'pagination' => $pagination, // Agrega los enlaces de paginación a la respuesta 'current_category' => $current_category, ]; // $response = array( // 'type' => 'success', // 'html' => ob_get_clean(), // 'page' => $paged // ); wp_reset_postdata(); else: __('No News'); $response = [ 'type' => 'failure', 'html' => 'No News', ]; endif; wp_send_json_success($response); wp_die(); } This second function is the one that fulfills pagination by page numbers. <?php add_action('wp_ajax_nopriv_pagination_ajax_custom', 'pagination_ajax_custom'); add_action('wp_ajax_filter_pagination_ajax_custom', 'pagination_ajax_custom'); function pagination_ajax_custom() { $paged_pagination = isset($_POST['page']) ? sanitize_text_field($_POST['page']) : ''; $categoria_pagination = isset($_POST['categoria']) ? sanitize_text_field($_POST['categoria']) : ''; $post_type_pagination = isset($_POST['post_type']) ? sanitize_text_field($_POST['post_type']) : ''; $taxonomy_pagination = isset($_POST['taxonomy']) ? sanitize_text_field($_POST['taxonomy']) : ''; $query_args_pagination = [ 'post_type' => $post_type_pagination, // 'posts_per_page' => $countPPP, 'post_status' => 'publish', 'order' => 'ASC', 'orderby' => 'date', 'posts_per_page' => '12', 'offset' => '0', 'paged' => $paged_pagination, // 'orderby' => 'date', 'tax_query' => [ [ 'taxonomy' => $taxonomy_pagination, 'field' => 'slug', 'terms' => $categoria_pagination, ], ], ]; $wp_query_pagination = new WP_Query($query_args_pagination); if ($wp_query_pagination->have_posts()): ob_start(); $categorias_pagination = []; while ($wp_query_pagination->have_posts()): $wp_query_pagination->the_post(); $term_pagination = get_the_terms( get_the_ID(), $taxonomy_pagination ); foreach ($term_pagination as $term_pagination) { $categorias_pagination[] = $term_pagination->slug; } ?> <div class="show-on-scroll item <?php echo $term_pagination->slug; ?>"> <a href="<?php the_permalink(); ?>"> <span class="videoMask"> <img src="<?php the_post_thumbnail_url('medium'); ?>" alt=""> </span> <div class="categoriaRecursoMultimedia <?php echo $term_pagination->slug; ?>"> <?php echo $term_pagination->name; ?> </div> <p><?php echo wp_trim_words(the_title(), 10, '...'); ?></p> </a> </div> <?php endwhile; $current_category_pagination = isset($term_pagination->slug) ? $term_pagination->slug : ''; $pagination_pagination = paginate_links([ 'total' => $wp_query_pagination->max_num_pages, 'current' => max(1, $paged_pagination), 'format' => '?paged=%#%', 'show_all' => false, 'type' => 'array', 'prev_next' => true, // 'end_size' => 4, 'prev_text' => __( '<img src="' . get_template_directory_uri() . '/images/icons8-atras.svg" />' ), 'next_text' => __( '<img src="' . get_template_directory_uri() . '/images/icons8-adelante.svg" />' ), ]); $response_pagination = [ 'type' => 'success', 'html' => ob_get_clean(), 'page' => $paged_pagination, 'pagination' => $pagination_pagination, // Agrega los enlaces de paginación a la respuesta 'current_category' => $current_category_pagination, ]; // $response = array( // 'type' => 'success', // 'html' => ob_get_clean(), // 'page' => $paged // ); wp_reset_postdata(); else: __('No News'); $response_pagination = [ 'type' => 'failure', 'html' => 'No News', ]; endif; wp_send_json_success($response_pagination); wp_die(); } JavaScript $(document).ready(function () { $(".filtroRecursoMultimedia").click(function () { // Traer todos los atributos que nos serviran para consultas con nuestras peticiones ajax const categorie = $(this).attr("data-categorie"); const taxonomy = $(this).attr("data-taxonomy"); const post_type = $(this).attr("data-post"); const ajaxURL = ajax_object.ajax_url; // Esta variable se obtiene automaticamente por wordpress, revisar la carga de este script en el archivo functions.php $.ajax({ url: ajaxURL, type: "POST", data: { // page: 1, action: "filter_ajax_multimedia", //nombre de la funcion de php categoria: categorie, }, error: function (jqXHR, textStatus, errorThrown) { // console.log('Error en la solicitud Ajax:', errorThrown); }, success: function (response) { switch (response.data.type) { case "success": // $('.sunset-load-more').attr('data-page', response.data.page); /* you need remove last five then use html method and if you want append then use append method */ $(".gridMultimedia").html(response.data.html); if (response.data.pagination) { const currentCategory = response.data.current_category; const paginationHtml = response.data.pagination .map((link) => { const page = $(link).text(); return $(link) .attr("data-page", page) .attr("data-current-category", currentCategory) .attr("data-post-type", post_type) .attr("data-taxonomy", taxonomy)[0].outerHTML; }) .join(""); $("#pagination-links-recursos").on( "click", ".page-numbers", function (event) { event.preventDefault(); const clickedPage = $(this).attr("data-page"); const clickedCategory = $(this).attr("data-current-category"); const clickedPostType = $(this).attr("data-post-type"); const clickedTaxonomy = $(this).attr("data-taxonomy"); ajax_pagination_query( clickedPostType, clickedTaxonomy, clickedCategory, clickedPage, ); }, ); $("#pagination-links-recursos").html(paginationHtml); } else { $("#pagination-links-recursos").html(""); } break; case "failure": $(".gridMultimedia").html(response.data.html); break; default: break; } }, }); }); // Función para páginacion function ajax_pagination_query(post_type, taxonomy, categoria, page) { $.ajax({ url: ajax_object.ajax_url, type: "POST", data: { action: "pagination_ajax_custom", //nombre de la funcion de php post_type: post_type, page: page, taxonomy: taxonomy, categoria: categoria, }, error: function (jqXHR, textStatus, errorThrown) {}, success: function (response) { switch (response.data.type) { case "success": // $('.sunset-load-more').attr('data-page', response.data.page); /* you need remove last five then use html method and if you want append then use append method */ $(".gridMultimedia").html(response.data.html); if (response.data.pagination) { const currentCategory = response.data.current_category; const paginationHtml = response.data.pagination .map((link) => { const page = $(link).text(); return $(link) .attr("data-page", page) .attr("data-current-category", currentCategory) .attr("data-post-type", post_type) .attr("data-taxonomy", taxonomy)[0].outerHTML; }) .join(""); $("#pagination-links-recursos").on( "click", ".page-numbers", function (event) { event.preventDefault(); }, ); $("#pagination-links-recursos").html(paginationHtml); } else { $("#pagination-links-recursos").html(""); } break; case "failure": $(".gridMultimedia").html(response.data.html); break; default: break; } }, }); } }); These are the buttons where I get the attributes that I use in the javascript: <button data-taxonomy="categoria" data-post="recursos_multimedia" data-categorie="todos" class="filtroRecursoMultimedia todos active">Todos los recursos multimedia</button> <?php $args = [ 'type' => 'recursos_multimedia', 'child_of' => 0, 'parent' => '', 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 1, 'hierarchical' => 1, 'taxonomy' => 'categoria', 'pad_counts' => false, ]; $categories = get_categories($args); foreach ($categories as $category) { $url = get_term_link($category); ?> <button data-taxonomy="categoria" data-post="recursos_multimedia" data-categorie="<?php echo $category->slug; ?>" class="filtroRecursoMultimedia <?php echo $category->slug; ?>"><?php echo $category->name; ?></button> <?php } ?> I checked in the console and I only got a 400 bad request error and in the response there is no more information than a "0", the payload is exactly what I need, I don't know what could be failing, I am checking very well and I can't find any logic to the error , I appreciate you helping me.
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.