Home › Forums › General Issues › AJAX Search w/ Relationship Field
I’ve stuck. How can I limit an AJAX search to only show posts in my relationship field? It’s currently coming back with results from all posts.
Query
function get_answer_keys($search_term = null, $tests = null) {
$tests = get_field('tests', false, false);
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'name',
'posts_per_page' => -1,
'post__in' => $tests
);
if (!empty($search_term)) {
$args['s'] = $search_term;
}
return new WP_Query($args);
}
Block-Answer_keys.php
<?php
/**
* Block Name: Related Section
*
* This is the template that displays a title + button + 4 blog posts.
*/
$search_term = get_query_var('search_term');
?>
<div class="filter">
<div class="search-bar">
<label for="answer_keys_search_term">Search</label>
<input type="text" id="answer_keys_search_term" placeholder="Test Code Search" value="<?php echo $search_term ?>"/>
</div>
</div>
<div class="ajax-container">
<div class="grid--25">
<?php
$query = get_answer_keys($search_term, $tests);
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
get_template_part('template-parts/content', 'test');
endwhile;
wp_reset_postdata();
else : ?>
<div class="alert"><?php _e('Sorry, no posts matched your criteria.', 'nashville-mdha'); ?></div>
<?php endif; ?>
</div>
</div>
Ajax.js
/** Answer Keys Search **/
$('#answer_keys_search_term').blur(function(e) {
getAnswerKeysSearchResults();
});
$('#answer_keys_search_term').keypress(function(e) {
var keycode = (e.keyCode ? e.keyCode : e.which);
if(keycode == '13') {
getAnswerKeysSearchResults();
}
});
function getAnswerKeysSearchResults() {
var search_term = $('#answer_keys_search_term').val();
$.ajax({
url: wpAjax.ajaxUrl,
data: { action: 'answer_keys_filter', search_term: search_term },
type: 'post',
beforeSend:function(xhr){
setFilterElementsProp(true);
},
success: function (result) {
setFilterElementsProp(false);
if (result) {
$('.ajax-container').html(result);
}
else {
// No result
$('.ajax-container').html('<div class="alert">Sorry, no results found.</div>');
}
},
error: function(result) {
setFilterElementsProp(false);
console.warn(result);
}
});
}
Results.php
add_action( 'wp_ajax_nopriv_answer_keys_filter', 'answer_keys_filter_ajax' );
add_action( 'wp_ajax_answer_keys_filter', 'answer_keys_filter_ajax' );
function answer_keys_filter_ajax() {
$search_term = htmlspecialchars($_POST['search_term']);
$query = get_answer_keys($search_term, $tests);
if($query->have_posts()) {
echo '<div class="grid--25">';
while($query->have_posts()) { $query->the_post();
get_template_part('template-parts/content', 'test');
}
echo '</div>';
} else {
?> <div class="alert">Sorry, but nothing matched your search criteria. Please try again with some different keywords.</div> <?php
}
wp_reset_postdata();
die();
}
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.