Hopefully I’m not causing any fuss by reopening this, but I’m just interested if you have any example of taking this approach that also allows for updating the post data via an AJAX call (or another way, I’m open to suggestions), where in my case the user is selecting some taxonomy terms on the page that determine which posts should then be displayed. I currently have a very similar setup to what @travis describes, where I’m initially doing the query to get all the posts (for initially displaying on the page) in controller, and then looping over them in my blade template.
I have a filter on the page rendered by the blade template, where the user can pick from some taxonomies which triggers an AJAX request. With AJAX I’m at a point where I can successfully get the new list of posts each time the user makes a selection. Where I’m a little lost is how exactly specific parts of either controller or the view itself (or perhaps a partial that gets data from the controller) can use this new data. Is there a way to update/refresh just a partial, passing it in new data based on the data I’m getting after each AJAX call?
Right now I’m just getting the new post data in functions php as I’m not sure how to set up my action in the AJAX differently.
Controller
public function case_studies_loop()
{
$case_study_items = get_posts([
'post_type' => 'case-study',
'posts_per_page'=>'10',
]);
return array_map(function ($post) {
return [
'heading' => apply_filters('the_title', $post->post_title),
'excerpt' => get_field('post_excerpt', $post),
'permalink' => get_permalink($post),
'thumbnail' => get_field('posts_additional_fields_thumbnail', $post),
];
}, $case_study_items);
}
Blade Template/View
@include('partials.our-work-gallery');
Partial (our-work-gallery.blade.php)
<div class="container taxonomy-filter-response">
<div class="row">
@foreach($case_studies as $case_study_item)
<a class="col-md-6 d-flex feed-col cpt-list-item" href="{!! $case_study_item['permalink'] !!}">
<div class="text-left blog-feed-post-entry-wrapper" style="background-image: url({!!
$case_study_item['thumbnail'] !!})">
<div class="post-title-wrap">
<p class="lead white feed-entry-title post-title">{!! $case_study_item['heading']!!}</p>
</div>
</div>
</a>
@endforeach
</div>
</div>
AJAX
$('.ajax-tax-select').change(function(e) {
var industryID = $('#industry-term').val();
var serviceID = $('#service-term').val();
var industryName = $('#industry-term').text()
var serviceName = $('#service-term').text();
$.ajax({
url: ajax_object.ajax_url,
type: 'POST',
data:{
'action':'do_ajax',
'fn':'update_case_studies',
'serviceID': serviceID,
'serviceName': serviceName,
'industryID': industryID,
'industryName': industryName,
},
dataType: 'json',
success:function(resp){
if(resp.success) {
console.log('success');
console.log('data: ', resp);
} else {
console.log('error');
}
},
error: printError,
});
e.preventDefault();
});
console.log(ajax_object);
var printError = function( req, status, err ) {
console.log( 'Hmm... something went wrong with the request: ', req, 'status: ', status, 'error: ', err );
alert( req.responseText);
};
},
The 'action':'do_ajax',
and 'fn':'update_case_studies',
work with a function added in functions.php that successfully runs on each AJAX call and gets the new list of posts:
functions.php
add_action('wp_ajax_nopriv_do_ajax', 'do_ajax_function');
add_action('wp_ajax_do_ajax', 'do_ajax_function');
function do_ajax_function() { //...do stuff to get new post data
Right now this is how I"m getting the data. The ...
above is where I run another query giving me the new list of posts to display. So now I have this new lists of posts in functions.php. But is there a way I can pass this data to udpate the partial, so the list of posts is displayed without a page reload?
I’m really not sure if I’m taking the right approach, and have some other issues with my AJAX call probably, but since my controller/view setup is very similar to what’s being discussed here, and I want to do it more dynamically, I wonder if anyone has any thoughts about the best way to do this. I made a post outliing some of my struggles here (sorry it is a bit messy, jumping directly to my latest reply might summarize it best).
Also feel free to close this/move it if somehow re-opening/replying is not the right thing to do here in the forums. I’m a bit new here and just wondering if anyone here on this thread might be able to point me toward a good resource/example or give me a hint on how to approach this.