admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('resource_filter_nonce')
]);
}
}
public function renderFilterForm() {
ob_start();
?>
Showing 0 resources
Filters applied: None
loadResources(); ?>
'resource',
// 'posts_per_page' => -1,
// 'tax_query' => [],
// 's' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '',
// ];
$search_term = isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '';
$query_args = [
'post_type' => 'resource',
'posts_per_page' => -1,
'tax_query' => ['relation' => 'OR'], // Allows matching by search OR taxonomy
'meta_query' => [
'relation' => 'OR',
[
'key' => 'post_title',
'value' => $search_term,
'compare' => 'LIKE'
],
[
'key' => 'post_content',
'value' => $search_term,
'compare' => 'LIKE'
]
]
];
if (!empty($search_term)) {
$query_args['tax_query'][] = [
'taxonomy' => 'resource_type',
'field' => 'name', // Search by the name of the taxonomy term
'terms' => $search_term,
'operator' => 'LIKE'
];
$query_args['tax_query'][] = [
'taxonomy' => 'resource_subject',
'field' => 'name',
'terms' => $search_term,
'operator' => 'LIKE'
];
}
if (!empty($_POST['resource_type'])) {
$query_args['tax_query'][] = [
'taxonomy' => 'resource_type',
'field' => 'slug',
'terms' => sanitize_text_field($_POST['resource_type'])
];
}
if (!empty($_POST['resource_subject'])) {
$query_args['tax_query'][] = [
'taxonomy' => 'resource_subject',
'field' => 'slug',
'terms' => sanitize_text_field($_POST['resource_subject'])
];
}
if (!empty($_POST['resource_type']) || !empty($_POST['resource_subject'])) {
$query_args['tax_query']['relation'] = 'AND';
if (!empty($_POST['resource_type'])) {
$query_args['tax_query'][] = [
'taxonomy' => 'resource_type',
'field' => 'slug',
'terms' => sanitize_text_field($_POST['resource_type'])
];
}
if (!empty($_POST['resource_subject'])) {
$query_args['tax_query'][] = [
'taxonomy' => 'resource_subject',
'field' => 'slug',
'terms' => sanitize_text_field($_POST['resource_subject'])
];
}
}
$query = new WP_Query($query_args);
ob_start();
if ($query->have_posts()) {
echo '';
while ($query->have_posts()) {
$query->the_post();
echo '
';
}
echo '
';
} else {
echo 'No resources found.
';
}
print_r($query_args);
wp_reset_postdata();
// Prepare response JSON
$response = [
'count' => $query->found_posts,
'filters' => [
'search' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '',
'resource_type' => !empty($_POST['resource_type']) ? sanitize_text_field($_POST['resource_type']) : '',
'resource_subject' => !empty($_POST['resource_subject']) ? sanitize_text_field($_POST['resource_subject']) : ''
],
'html' => ob_get_clean()
];
echo json_encode($response);
wp_die();
}
private function loadResources($query_args = ['post_type' => 'resource', 'posts_per_page' => -1]) {
$query = new WP_Query($query_args);
if ($query->have_posts()) {
echo '';
while ($query->have_posts()) {
$query->the_post();
echo '
';
}
echo '
';
} else {
echo 'No resources found.
';
}
wp_reset_postdata();
}
}
new ResourceFilterPlugin();