Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //The PHP WordPress Filter, added in function.php
- function filter_products() {
- $catIds = $_POST['catIds'];
- $tagIds = $_POST['tagIds'];
- $args = [
- 'post_type' => 'post',
- 'posts_per_page' => -1,
- 'post_status' => 'publish',
- 'orderby' => 'date',
- 'order' => 'desc',
- ];
- // project Category
- if (count($catIds) > 1) {
- $args['tax_query'][] = [
- 'taxonomy' => 'category',
- 'field' => 'term_id',
- 'terms' => $catIds,
- 'operator' => 'IN'
- ];
- }
- // project tag
- if (count($tagIds) > 1) {
- $args['tax_query'][] = [
- 'taxonomy' => 'post_tag',
- 'field' => 'term_id',
- 'terms' => $tagIds,
- 'operator' => 'IN'
- ];
- }
- $ajaxproducts = new WP_Query($args);
- $response = '';
- if ( $ajaxproducts->have_posts() ) {
- ob_start();
- while ( $ajaxproducts->have_posts() ) : $ajaxproducts->the_post();
- $response .= '<li>'.the_title().'</li>';
- endwhile;
- $output = ob_get_contents();
- ob_end_clean();
- } else {
- echo __( 'No projects found' );
- }
- $result = [
- 'total' => $counter,
- 'html' => $output,
- ];
- echo json_encode($result);
- wp_reset_postdata();
- exit;
- }
- add_action('wp_ajax_filter_products', 'filter_products');
- add_action('wp_ajax_nopriv_filter_products', 'filter_products');
Advertisement
Add Comment
Please, Sign In to add comment