-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax-cpt-taxonomy-filter.php
187 lines (156 loc) · 5.17 KB
/
ajax-cpt-taxonomy-filter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* Plugin Name: Ajax CPT and Taxonomy Filter
* Plugin URI: #
* Description: Handle Ajax Search and Filter For CPT and Taxonomy
* Version: 1.0.0
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: Shahala Anjum
* Author URI: #
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: ajax-cpt-taxonomy-filter
*/
//enque scripts
function ajax_filter_enqueues() {
wp_enqueue_script( 'filter', plugins_url( 'filter.js', __FILE__ ));
wp_localize_script( 'ajax-search', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'ajax_filter_enqueues' );
$atts = shortcode_atts(
array(
'postype' => 'post',
'taxtype' => 'category',
), $atts, 'ajax_posts_filter' );
function ajax_sht($atts){
?>
<div class="container">
<div class="portfolio-categories-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="">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">
<?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'];
$tagIds = $_POST['tagIds'];
$page = (isset($_POST['pageNumber'])) ? $_POST['pageNumber'] : 0;
$args = [
'post_type' => 'post',
'posts_per_page' => 8,
'post_status' => 'publish',
'orderby' => 'publish_date',
'paged' => $page,
];
// project Category
if (!empty($catIds)) {
$args['tax_query'][] = [
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $catIds,
'operator' => 'IN'
];
}
// project tag
if (!empty($tagIds)) {
$args['tax_query'][] = [
'taxonomy' => 'post_tag',
'field' => 'term_id',
'terms' => $tagIds,
'operator' => 'IN'
];
}
$response = '';
$ajaxproducts = new WP_Query($args);
if ( $ajaxproducts->have_posts() ) {
ob_start();
while ( $ajaxproducts->have_posts() ) : $ajaxproducts->the_post();
// $response = get_template_part('page-templates/page/content/content', 'page');
//echo $response = get_the_title().','; ?>
<div class="news_blog_module_list">
<figure class="news_blog_img" style="background-image: url(<?php the_field('featured_image'); ?>);">
<!-- IMage define in BG -->
<a href="<?php echo get_permalink(); ?>" title=""><!--link Define Here --></a>
</figure>
<?php
$postcategories = get_the_category();
$category_list = join( ', ', wp_list_pluck( $postcategories, 'name' ) );
?>
<!-- <h5><?php //echo wp_kses_post( $category_list ); ?></h5> -->
<h3><a href="<?php echo get_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
</div>
<?php
endwhile;
$output = ob_get_contents();
ob_end_clean();
} else {
$output = '<h1 class="no-posts">No posts found</h1>' ;
$no_projects = 1;
}
$current = get_query_var( 'paged' ) ? (int) get_query_var( 'paged' ) : 1;
$total = isset( $ajaxproducts->max_num_pages ) ? $ajaxproducts->max_num_pages : 1;
$counter = $ajaxproducts->max_num_pages;
$result = [
'current' => $current,
'total' => $total,
'html' => $output,
'no_projects' => $no_projects,
];
echo json_encode($result);
wp_reset_postdata();
exit;
}
add_action('wp_ajax_filter_blogs', 'filter_blogs');
add_action('wp_ajax_nopriv_filter_blogs', 'filter_blogs');