Issue
This Content is from Stack Overflow. Question asked by Kevin
I’m new in WordPress and I’m doing a project in my university,
I would like to know if there’s a way to prevent WordPress or Woocommerce displaying the products automatically in the category, store and search pages?
Have been reading a lot of threads about Ajax filters and pagination, but can’t really get it.
Let me show you the way I’m trying to achieve it.
I created a file called ajax.php in root directory, this is the code:
function filterAjax(){
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'pa_rin',
'field' => 'slug',
'terms' => 'R16',
'operator' => 'IN'
),
)
);
$loop = new wp_query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$products[0]= [
"id" => $product->get_id(),
"link" => $product->get_permalink(),
"title" => $product->get_title(),
"type" => $product->get_type(),
"status" => $product->get_status(),
"visibility" => $product->get_catalog_visibility(),
"price" => $product->get_price(),
"categories" => $product->get_categories(),
"image" => $product->get_image()
];
endwhile;
$products[1] = [
"products quantity" => $loop->found_posts()
];
wp_reset_query();
echo json_encode($productos);}
Don’t pay atention to $products[0] in the loop, just needed 1 product to know if it works and it does.
Also I got the quantity of products, but can’t make work the $paged work, example if I add the following
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
'paged' => $paged,
$current_page = max( 1, get_query_var('paged') );
Always returns current page = 1, it’s because this file isn’t in the products page?
What’s exactly var ‘paged’? something like get_query_var(‘/page/3/’)?
I would like to control everything with javascript and that ajax.php file, since I don’t know how to exactly work with wordpress.
For example, an user press number 3 in pagination and I catch it with js, then I send/tell to ajax.php get page 3, how could I do a query to get the products of the 3rd page?
By the way, I’m not a native english speaker.
Solution
This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.