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}
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});
1jQuery(document).ready( function() {
2
3 jQuery(".user_vote").click( function(e) {
4 e.preventDefault();
5 post_id = jQuery(this).attr("data-post_id")
6 nonce = jQuery(this).attr("data-nonce")
7
8 jQuery.ajax({
9 type : "post",
10 dataType : "json",
11 url : myAjax.ajaxurl,
12 data : {action: "my_user_vote", post_id : post_id, nonce: nonce},
13 success: function(response) {
14 if(response.type == "success") {
15 jQuery("#vote_counter").html(response.vote_count)
16 }
17 else {
18 alert("Your vote could not be added")
19 }
20 }
21 })
22
23 })
24
25})
1echo '<button id="ZapisPrace">Save</button>
2<script>
3jQuery("#ZapisPrace").click(function($){
4 var data={
5 action: "addToDB",
6 info: "nomz"
7 };
8 jQuery.post(ajaxurl,data,function(response){
9 alert("Response was "+ response);
10 });
11});
12</script>';
13add_action('wp_ajax_addToDB','pridajDoDB');
14function pridajDoDB(){
15 echo '<script>console.log("AAA")</script>';
16 wp_die();
17}