Guest User

portfolio function

a guest
Nov 13th, 2021
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.30 KB | None | 0 0
  1.  
  2. //The PHP WordPress Filter, added in function.php
  3.  
  4. function filter_products() {
  5.     $catIds = $_POST['catIds'];
  6.     $tagIds = $_POST['tagIds'];
  7.  
  8.     $args = [
  9.         'post_type' => 'post',
  10.         'posts_per_page' => -1,
  11.         'post_status'  => 'publish',
  12.         'orderby'        => 'date',
  13.         'order'          => 'desc',
  14.     ];
  15.     // project Category
  16.     if (count($catIds) > 1) {
  17.         $args['tax_query'][] = [
  18.             'taxonomy'      => 'category',
  19.             'field'         => 'term_id',
  20.             'terms'         => $catIds,
  21.             'operator'      => 'IN'
  22.         ];
  23.     }
  24.     // project tag
  25.     if (count($tagIds) > 1) {
  26.         $args['tax_query'][] = [
  27.             'taxonomy'      => 'post_tag',
  28.             'field'         => 'term_id',
  29.             'terms'         => $tagIds,
  30.             'operator'      => 'IN'
  31.         ];
  32.     }
  33.     $ajaxproducts = new WP_Query($args);
  34.     $response = '';
  35.     if ( $ajaxproducts->have_posts() ) {
  36.         ob_start();
  37.         while ( $ajaxproducts->have_posts() ) : $ajaxproducts->the_post();
  38.             $response .= '<li>'.the_title().'</li>';
  39.         endwhile;
  40.         $output = ob_get_contents();
  41.         ob_end_clean();
  42.     } else {
  43.         echo __( 'No projects found' );
  44.     }
  45.        
  46.     $result = [
  47.         'total' => $counter,
  48.         'html' => $output,
  49.     ];
  50.    
  51.     echo json_encode($result);
  52.     wp_reset_postdata();
  53.     exit;
  54. }
  55. add_action('wp_ajax_filter_products', 'filter_products');
  56. add_action('wp_ajax_nopriv_filter_products', 'filter_products');
  57.  
Advertisement
Add Comment
Please, Sign In to add comment