How to run wp query with multiple values
-
Hy, I am trying to run an ajax call and getting data from server.
I have 3 options on frontend
1: price high to low
2: price low to high
3: under 20000
I have set an attribute of ‘data-value’ to each of the value so i can identify the data in backend.
When the button is clicked it will pick their data-value attribute value and will check which button is clicked and will set variable values according to that
I have this code in my custom.js
let x = $('.listing-offer-optionn-link') $(x).click(function() { alert('loading'); let valu = ''; let priceVal = null; let priceKey = null ; let x = $(this).attr('data-value') switch (x) { case 'low-to-high': valu = 'ASC'; break; case 'high-to-low': valu = 'DESC'; break; default: valu = 'ASC'; priceVal = '20000'; priceKey = 'price'; } $.ajax({ type : 'POST', datatype : 'html', url : 'https://localhost/valueautos/wp-admin/admin-ajax.php', data : { 'filter' : valu, 'priceFilter': priceVal, 'priceKey' : priceKey, 'action' : 'Get_Filters' }, success : function(result) { console.log(result) $('.related-listing-listings-wrapper').html(result) }, error : function(error) { alert('error in getting record') } }) })
and I have this code in my functions.php
add_action( 'wp_ajax_Get_Filters', 'Get_Filters_callback' ); add_action( 'wp_ajax_nopriv_Get_Filters', 'Get_Filters_callback' ); function Get_Filters_callback() { $to = $_POST['filter']; // The Query $args = array( 'post_type' => 'listings', 'posts_per_page' => 50, 'meta_key' => 'price', 'order' => $_POST['filter'] , 'orderby' => 'meta_value_num', 'meta_query' => array( array( 'key' => $_POST['priceKey'], 'value' => $_POST['priceFilter'] , 'compare' => '<=', ), ) ); $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) { echo '<ul>'; while ( $the_query->have_posts() ) { // post content } }
now the problem is I am getting correct posts when i click on button (under 20000) but i get nothing when i click on ‘price low to high’ or ‘price high to low’
Any suggestions what I am doing wrong?
- The topic ‘How to run wp query with multiple values’ is closed to new replies.