I have a page that lists all custom posts of type “the-talent”. I have three Dropdowns each with a custom taxonomy (level, location, and specialty). I have a on-click function that grabs the selected values from the dropdown. Each time someone makes a selection I’d like to run a new query with those inputs. Ideally I’d like to do this with ajax and just refresh the section with the posts, but I’m having a hell of a time trying to figure out how to use ajax inside wordpress. I followed a couple tutorials (https://premium.wpmudev.org/blog/load-posts-ajax/) and created a separate js file and added hooks inside the function file as the tutorial suggested, but my ajax never made it back from the server (admin-ajax.php). Not sure what other options are available.
So now I’m back to having everything inside my page-talent.php file.
here is my JS script that grabs the values. at the end of it are the 3 values for (level, location, and specialty).
$(".dropdown-menu li a").click(function (event) {
var $this = $(this)
var $dropdown = $this.parents('.dropdown')
var html = $this.text() + ' <span class="caret"></span>'
$dropdown.find(".btn").html(html);
$dropdown.find('a').removeClass('checked');
$this.addClass('checked');
var filters = getFilters();
//var level = filters["level"];
var level = filters.level;
var location = filters.location;
var specialty = filters.specialty;
alert("Level: "+level+" Location: "+location+" Specialty: "+specialty);
console.log(getFilters());
event.preventDefault();
$.ajax({
url: ajaxpagination.ajaxurl,
type: 'post',
data: {
action: 'ajax_pagination'
},
success: function( result ) {
alert("result"+ result );
}
})// end ajax
})
Here is my working php for the query. I’m trying to foggier out how to add the JS variables to the query below and rerun the query.
$args = array(
'post_type' => 'the-talent',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'level',
'field' => 'term_id',
'terms' => array( '25' ),// replace with var level
),
array(
'taxonomy' => 'location',
'field' => 'term_id',
'terms' => array( '20' ),// replace with var location
),
array(
'taxonomy' => 'specialty',
'field' => 'term_id',
'terms' => array( '4' ),// replace with var specialty
),
),
);
$talent = new WP_Query( $args );
any suggestions? Any help would be greatly appreciated.