// Function to search posts
function searchPosts(query) {
const searchTerm = query.toLowerCase().trim();
// Reset filters when searching
currentCategoryFilter = 'all';
currentCountryFilter = 'all';
if (searchTerm === '') {
applyFilters();
return;
}
const filtered = allPosts.filter(post => {
const title = post.title.rendered.toLowerCase();
const description = post.excerpt ? stripHtml(post.excerpt.rendered).toLowerCase() : '';
return title.includes(searchTerm) || description.includes(searchTerm);
});
displayPosts(filtered);
}
// Load groups when page loads
document.addEventListener('DOMContentLoaded', () => {
loadWhatsAppGroups();
// Add category filter listeners
document.querySelectorAll('#category-buttons .filter-btn').forEach(btn => {
btn.addEventListener('click', function() {
// Update active state for category buttons only
document.querySelectorAll('#category-buttons .filter-btn').forEach(b => b.classList.remove('active'));
this.classList.add('active');
currentCategoryFilter = this.getAttribute('data-filter');
document.getElementById('search-input').value = '';
applyFilters();
});
});
// Add country filter listeners
document.querySelectorAll('#country-buttons .filter-btn').forEach(btn => {
btn.addEventListener('click', function() {
// Update active state for country buttons only
document.querySelectorAll('#country-buttons .filter-btn').forEach(b => b.classList.remove('active'));
this.classList.add('active');
currentCountryFilter = this.getAttribute('data-filter');
document.getElementById('search-input').value = '';
applyFilters();
});
});
// Add search input listener
const searchInput = document.getElementById('search-input');
searchInput.addEventListener('input', (e) => {
// Reset all filter buttons to "All" when searching
document.querySelectorAll('#category-buttons .filter-btn').forEach(btn => {
btn.classList.remove('active');
if (btn.getAttribute('data-filter') === 'all') btn.classList.add('active');
});
document.querySelectorAll('#country-buttons .filter-btn').forEach(btn => {
btn.classList.remove('active');
if (btn.getAttribute('data-filter') === 'all') btn.classList.add('active');
});
searchPosts(e.target.value);
});
});