wordpress ajax filter code

Solutions on MaxInterview for wordpress ajax filter code by the best coders in the world

showing results for - "wordpress ajax filter code"
Gabriel
31 Jul 2020
1add_action('wp_ajax_myfilter', 'misha_filter_function'); // wp_ajax_{ACTION HERE} 
2add_action('wp_ajax_nopriv_myfilter', 'misha_filter_function');
3 
4function misha_filter_function(){
5	$args = array(
6		'orderby' => 'date', // we will sort posts by date
7		'order'	=> $_POST['date'] // ASC or DESC
8	);
9 
10	// for taxonomies / categories
11	if( isset( $_POST['categoryfilter'] ) )
12		$args['tax_query'] = array(
13			array(
14				'taxonomy' => 'category',
15				'field' => 'id',
16				'terms' => $_POST['categoryfilter']
17			)
18		);
19 
20	// create $args['meta_query'] array if one of the following fields is filled
21	if( isset( $_POST['price_min'] ) && $_POST['price_min'] || isset( $_POST['price_max'] ) && $_POST['price_max'] || isset( $_POST['featured_image'] ) && $_POST['featured_image'] == 'on' )
22		$args['meta_query'] = array( 'relation'=>'AND' ); // AND means that all conditions of meta_query should be true
23 
24	// if both minimum price and maximum price are specified we will use BETWEEN comparison
25	if( isset( $_POST['price_min'] ) && $_POST['price_min'] && isset( $_POST['price_max'] ) && $_POST['price_max'] ) {
26		$args['meta_query'][] = array(
27			'key' => '_price',
28			'value' => array( $_POST['price_min'], $_POST['price_max'] ),
29			'type' => 'numeric',
30			'compare' => 'between'
31		);
32	} else {
33		// if only min price is set
34		if( isset( $_POST['price_min'] ) && $_POST['price_min'] )
35			$args['meta_query'][] = array(
36				'key' => '_price',
37				'value' => $_POST['price_min'],
38				'type' => 'numeric',
39				'compare' => '>'
40			);
41 
42		// if only max price is set
43		if( isset( $_POST['price_max'] ) && $_POST['price_max'] )
44			$args['meta_query'][] = array(
45				'key' => '_price',
46				'value' => $_POST['price_max'],
47				'type' => 'numeric',
48				'compare' => '<'
49			);
50	}
51 
52 
53	// if post thumbnail is set
54	if( isset( $_POST['featured_image'] ) && $_POST['featured_image'] == 'on' )
55		$args['meta_query'][] = array(
56			'key' => '_thumbnail_id',
57			'compare' => 'EXISTS'
58		);
59	// if you want to use multiple checkboxed, just duplicate the above 5 lines for each checkbox
60 
61	$query = new WP_Query( $args );
62 
63	if( $query->have_posts() ) :
64		while( $query->have_posts() ): $query->the_post();
65			echo '<h2>' . $query->post->post_title . '</h2>';
66		endwhile;
67		wp_reset_postdata();
68	else :
69		echo 'No posts found';
70	endif;
71 
72	the ( ) ;
73}
Stan
22 Jan 2020
1jQuery(function($){
2	$('#filter').submit(function(){
3		var filter = $('#filter');
4		$.ajax({
5			url:filter.attr('action'),
6			data:filter.serialize(), // form data
7			type:filter.attr('method'), // POST
8			beforeSend:function(xhr){
9				filter.find('button').text('Processing...'); // changing the button label
10			},
11			success:function(data){
12				filter.find('button').text('Apply filter'); // changing the button label back
13				$('#response').html(data); // insert data
14			}
15		});
16		return false;
17	});
18});
queries leading to this page
wp ajax filter for post titlewordpress using ajax to filter postshow to use ajax in wordpress using jsfilters for custom post type ajaxwordpress ajax filter posts by custom fieldsjquery filter in javascript in wordpresswordpress categories search ajaxfilter ajax wordpress pluginwp ajax wordpress documentation show posts by custom fields ajax filter3 steps to create ajax post filtersajax wpquery filterhow to use ajax wordpresswordpress ajax callwordpress ajax filterajax php wordpresshow to ajax in wordpressuse ajax in wordpress wordpress ajax post filter dropdown without pluginhow to use ajax for wordpress websitewordpress blocks ajax filterwordpress jquery ajax examplemake ajax calls in wordpress pagewordpress ajax api callwordpress ajax request in 22javascript 22 instead of jquerywordpress ajax filter querywordpress ajax filter posts by category tutorialapply filter on ajax result wordspressadd html ajax filter wordpressajax filter plugin wordpresswordpress add ajax callbackwordpress ajax request in 22javascript 22ajax request wordpressadd ajax in wordpresshow to create ajax filter in wordpresssimple ajax category filter wordpressajax calls on wordpresswordpress api filter ajax callwordpress create ajax filterhow to use ajax in wordpresswordpress filter posts with ajaxwordpress custom ajax filterwordpress filter posts by category ajaxhow to handle ajax in wordpressajax filter in wordpress hookhow to use ajax to filter in wordpresswordpress ajax requestwordpress ajax request in javascripton ajax call filter wordpresswordpress ajax return codewordpress ajax get requesthow to call ajax in wordpressajax name filterwordpress ajax functionwordpress do wp ajax callajax example wordpresswordpress jquery ajaxwordpress ajax demowordpress ajax post filter pluginwordpress ajax filter cptajax and wordpresswordpress filter ajax postajax step by step filter wordpressajax call in wordpress programmaticallyajax post filterbutton to filter post by category in wordpressbest way to call ajax wordpresshow to wordpress ajaxajax filtering wordpresswordpress ajax for filterajax wordpresswordpress ajax in wordpress code customcreate post filter htmlcreate ajax api wordpressajax call in wordpressajax in wordpressajax call wordpressmake a ajax in wordpresscategory filter wordpress code ajaxwordpress ajax examplefilter in wordpress using ajax 24 ajax wordpresswordpress ajax post to websitewp ajax in wordpresswordpress ajax filter and pagnationwordpress ajax in wordpressajax function wordpresswp ajax filter postsajax request php function in wordpress pluginajax request in wordpress plugin developmentwp ajax filterswordpress request ajaxwordpress blocks ajax filter pluginusing ajax in wordpresswordpress plugin ajax posthow to do wordpress ajaxajax filter in wordpresswordpress filter items ajaxwordpress ajax post filter selectajax filter wordpresswordpress ajax post filtercall filter on ajax wordpresswordpress ajax handlerwordpress ajax filterswp ajax filter actionsajax filter with visual wordpresswordpress ajaxwordpress ajax postwordpress ajax call in function phpcall wordpress filter ajaxwordpress ajax call php functionwordpress ajax filter codewordpress plugin ajaxj filterwp ajax requestwp ajax in wordpresshow to call php file using ajax in wordpresscreate ajax request function in functions php 2b wordpressajax taxonomy filter wordpresswordpress ajax filters for custom taxonomywordpress ajax tutorialfilter post by category wordpress ajax codejquery filter for wordpresswordpress ajax filter code