Advertisement
Guest User

function

a guest
Dec 14th, 2021
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.47 KB | None | 0 0
  1.  
  2. $atts = shortcode_atts(
  3.     array(
  4.         'postype' => 'post',
  5.         'taxtype' => 'category',
  6.     ), $atts, 'ajax_posts_filter' );
  7.  
  8. function ajax_sht($atts){
  9.  
  10.          ?>
  11.     <div class="container tax-data">
  12.         <div class="taxonomy-list">
  13.        
  14.            
  15.            
  16.             <?php
  17.             if(!empty($atts['taxtype']) ){
  18.  
  19.                 //passing more than one value with comma seperated
  20.                 $no_whitespaces = preg_replace( '/\s*,\s*/', ',', filter_var( $atts['taxtype'], FILTER_SANITIZE_STRING ) );
  21.                 $tax_array = explode( ',', $no_whitespaces );
  22.  
  23.                 foreach($tax_array as $onetax){
  24.                    
  25.                     $categories = get_terms( $onetax, 'hide_empty=0');
  26.                     // echo '<pre>';
  27.                     // print_r($categories);
  28.                     echo '
  29.                    
  30.                         <input type="hidden" id="filters-'.$onetax.'" />
  31.                         <ul class="'.$onetax.'-list ">' ;  ?>
  32.                             <li><a href="javascript:;" class="filter-link portfolio-cat-item cat-list_item active" data-slug="" data-id="" data-type="">All </a></li>
  33.                             <?php foreach($categories as $category) : ?>
  34.                                 <li>
  35.                                     <a href="javascript:;" class="filter-link cat-list_item" data-slug="<?= $category->slug; ?>" data-type="<?php echo $onetax; ?>" data-id="<?= $category->term_id; ?>">
  36.                                         <?= $category->name; ?>
  37.                                     </a>
  38.                                     <span class="remove"><i class="fas fa-times"></i></span>
  39.                                 </li>
  40.                             <?php endforeach; ?>
  41.                         </ul>
  42.                    
  43.                 <?php }
  44.             ?>
  45.            
  46.             <?php } ?>
  47.         </div>
  48.         <div class="projects-grid" post-type="<?php echo $atts['postype']; ?>">
  49.             <?php
  50.             $projects = new WP_Query([
  51.                 'post_type' => $atts['postype'],
  52.                 'posts_per_page' => 4,
  53.                 'order_by' => 'name',
  54.             ]);
  55.             ?>
  56.             <div class="count" id="result-count"></div>
  57.             <?php if($projects->have_posts()): ?>
  58.                 <ul class="project-tiles-portfolio project-tiles">
  59.                     <?php
  60.                     while($projects->have_posts()) : $projects->the_post();
  61.                         echo '<li>';
  62.                             the_title();
  63.                         echo '</li>';
  64.                     endwhile;
  65.                     ?>
  66.                 </ul>
  67.                 <?php wp_reset_postdata(); ?>
  68.                 <div id="more_blog_posts">Load More</div>
  69.                
  70.             <?php
  71.             else :
  72.                 echo 'No posts found';
  73.             endif; ?>
  74.         </div>
  75.     </div>
  76. <?php
  77. }
  78.  
  79. add_shortcode( 'ajax_posts_filter', 'ajax_sht' );
  80.  //The PHP WordPress Filter,
  81.  
  82. function filter_blogs() {
  83.     $catIds = $_POST['catIds'];
  84.     $postype = $_POST['postype'];
  85.     $taxType = $_POST['taxType'];
  86.    
  87.     //$tagIds = $_POST['tagIds'];
  88.     $page = (isset($_POST['pageNumber'])) ? $_POST['pageNumber'] : 0;
  89.     $args = [
  90.         'post_type' => ($postype) ? $postype : 'post',
  91.         'posts_per_page' => 4,
  92.         'post_status'  => 'publish',
  93.         'orderby'        => 'publish_date',
  94.         'order'     => ASC ,
  95.         'paged'    => $page,
  96.     ];
  97.     // project Category
  98.     if (!empty($catIds)) {
  99.         $args['tax_query'][] = [
  100.             'taxonomy'      => $taxType,
  101.             'field'         => 'term_id',
  102.             'terms'         => $catIds,
  103.             'operator'      => 'IN'
  104.         ];
  105.     }
  106.  
  107.     $output = '';
  108.     $ajaxposts = new WP_Query($args);
  109.  
  110.     if ( $ajaxposts->have_posts() ) {
  111.         $output .=  $atts['postype'].'something';
  112.         while ( $ajaxposts->have_posts() ) : $ajaxposts->the_post();
  113.         $output .= get_the_title().'<br> from ajax';
  114.             //echo get_the_ID().',';
  115.         endwhile;  
  116.         $counter = $ajaxposts->max_num_pages;
  117.         $result = [
  118.             'total' => $counter,
  119.             'html' => $output,
  120.         ]; 
  121.     } else {
  122.         $result = [
  123.             'total' => $counter,
  124.             'html' => 'No projects',
  125.         ]; 
  126.     }
  127.     echo json_encode($result);
  128.     wp_reset_postdata();
  129.     die;
  130. }
  131. add_action('wp_ajax_filter_blogs', 'filter_blogs');
  132. add_action('wp_ajax_nopriv_filter_blogs', 'filter_blogs');
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement