showing results for - "wordpress ajax request"
Miguel
09 Jan 2017
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}
Alejandra
11 Apr 2018
1function my_enqueue() {
2      wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/my-ajax-script.js', array('jquery') );
3      wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
4 }
5 add_action( 'wp_enqueue_scripts', 'my_enqueue' );
6
7//After you can do in js
8jQuery.ajax({
9    type: "post",
10    dataType: "json",
11    url: my_ajax_object.ajax_url,
12    data: formData,
13    success: function(msg){
14        console.log(msg);
15    }
16});