Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $atts = shortcode_atts(
- array(
- 'postype' => 'post',
- 'taxtype' => 'category',
- ), $atts, 'ajax_posts_filter' );
- function ajax_sht($atts){
- ?>
- <div class="container tax-data">
- <div class="taxonomy-list">
- <?php
- if(!empty($atts['taxtype']) ){
- //passing more than one value with comma seperated
- $no_whitespaces = preg_replace( '/\s*,\s*/', ',', filter_var( $atts['taxtype'], FILTER_SANITIZE_STRING ) );
- $tax_array = explode( ',', $no_whitespaces );
- foreach($tax_array as $onetax){
- $categories = get_terms( $onetax, 'hide_empty=0');
- // echo '<pre>';
- // print_r($categories);
- echo '
- <input type="hidden" id="filters-'.$onetax.'" />
- <ul class="'.$onetax.'-list ">' ; ?>
- <li><a href="javascript:;" class="filter-link portfolio-cat-item cat-list_item active" data-slug="" data-id="" data-type="">All </a></li>
- <?php foreach($categories as $category) : ?>
- <li>
- <a href="javascript:;" class="filter-link cat-list_item" data-slug="<?= $category->slug; ?>" data-type="<?php echo $onetax; ?>" data-id="<?= $category->term_id; ?>">
- <?= $category->name; ?>
- </a>
- <span class="remove"><i class="fas fa-times"></i></span>
- </li>
- <?php endforeach; ?>
- </ul>
- <?php }
- ?>
- <?php } ?>
- </div>
- <div class="projects-grid" post-type="<?php echo $atts['postype']; ?>">
- <?php
- $projects = new WP_Query([
- 'post_type' => $atts['postype'],
- 'posts_per_page' => 4,
- 'order_by' => 'name',
- ]);
- ?>
- <div class="count" id="result-count"></div>
- <?php if($projects->have_posts()): ?>
- <ul class="project-tiles-portfolio project-tiles">
- <?php
- while($projects->have_posts()) : $projects->the_post();
- echo '<li>';
- the_title();
- echo '</li>';
- endwhile;
- ?>
- </ul>
- <?php wp_reset_postdata(); ?>
- <div id="more_blog_posts">Load More</div>
- <?php
- else :
- echo 'No posts found';
- endif; ?>
- </div>
- </div>
- <?php
- }
- add_shortcode( 'ajax_posts_filter', 'ajax_sht' );
- //The PHP WordPress Filter,
- function filter_blogs() {
- $catIds = $_POST['catIds'];
- $postype = $_POST['postype'];
- $taxType = $_POST['taxType'];
- //$tagIds = $_POST['tagIds'];
- $page = (isset($_POST['pageNumber'])) ? $_POST['pageNumber'] : 0;
- $args = [
- 'post_type' => ($postype) ? $postype : 'post',
- 'posts_per_page' => 4,
- 'post_status' => 'publish',
- 'orderby' => 'publish_date',
- 'order' => ASC ,
- 'paged' => $page,
- ];
- // project Category
- if (!empty($catIds)) {
- $args['tax_query'][] = [
- 'taxonomy' => $taxType,
- 'field' => 'term_id',
- 'terms' => $catIds,
- 'operator' => 'IN'
- ];
- }
- $output = '';
- $ajaxposts = new WP_Query($args);
- if ( $ajaxposts->have_posts() ) {
- $output .= $atts['postype'].'something';
- while ( $ajaxposts->have_posts() ) : $ajaxposts->the_post();
- $output .= get_the_title().'<br> from ajax';
- //echo get_the_ID().',';
- endwhile;
- $counter = $ajaxposts->max_num_pages;
- $result = [
- 'total' => $counter,
- 'html' => $output,
- ];
- } else {
- $result = [
- 'total' => $counter,
- 'html' => 'No projects',
- ];
- }
- echo json_encode($result);
- wp_reset_postdata();
- die;
- }
- add_action('wp_ajax_filter_blogs', 'filter_blogs');
- add_action('wp_ajax_nopriv_filter_blogs', 'filter_blogs');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement