Ecommerce Shopify WordPress Discussion

filter the custom post type using wp ajax request with form submission

I want to filter the custom post type (events) using wp ajax request on behalf of the keyword supplied to it. Explanation: I have a from which has 4 fields displaying search keyword, event category (custom taxonomy), year (custom meta field - mapped to event live date meta field), event tags (custom taxonomy). Every custom post type having these information while data entry post title post content event live date event tags event category a custom post title for display it at single page Problem Statement: When user filters using form with keyword (if all field are empty only keyword is set): then the keyword should search in post_title OR post_content OR _custom_post_title If user used any filter except event year along with keyword - then it should search in all in following : (post_title OR post_content OR _custom_post_title) AND event_category AND event_tags if user select all filters even event year too, then search keyword should search in (post_title OR post_content) AND event_category AND event_live_date AND event_tags and in the end all posts should order by event_live_date in DESC order with or without the search filters. Below is the code i have used the wp_ajax function where I am preparing the wp_query arguments, but is it not working as above expectation. add_action('wp_ajax_filter_events', 'filter_events_data'); add_action('wp_ajax_nopriv_filter_events', 'filter_events_data'); function filter_events_data() { check_ajax_referer('filter_posts', 'nonce'); $paged = isset($_POST['current_page']) ? absint($_POST['current_page']) : 1; $posts_per_page = $_POST['posts_per_page']; $args = array( 'post_type' => 'events', 'post_status' => 'publish', 'posts_per_page' => $posts_per_page, 'paged' => $paged, 'orderby' => 'meta_value', 'order' => 'DESC', 'meta_query' => array( 'relation' => 'AND', array( 'relation' => 'OR', array( 'key' => 'event_live_date', 'compare' => 'EXISTS', ), array( 'key' => 'event_live_date', 'compare' => 'NOT EXISTS', ), ), ), ); if (isset($_POST['isFormSubmitted']) && $_POST['isFormSubmitted'] == true) { $raw_data = isset($_POST['formData']) ? $_POST['formData'] : ''; $form_data = array(); wp_parse_str($raw_data, $form_data); // Now prepare the taxonomy statement. $tax_query = array('relation' => 'AND'); // If filter by event type is set if (!empty($form_data['filter_by_eventType'])) { $tax_query[] = array( 'taxonomy' => 'events-category', 'field' => 'slug', 'terms' => sanitize_text_field($form_data['filter_by_eventType']), ); } // If filter by event tags is set if (!empty($form_data['filter_by_eventTags'])) { $tax_query[] = array( 'taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => sanitize_text_field($form_data['filter_by_eventTags']), ); } //if filter by event year is set if (!empty($form_data['filter_by_eventYear'])) { $event_published_date_query = array( 'key' => 'event_live_date', 'value' => sanitize_text_field($form_data['filter_by_eventYear']), 'compare' => 'LIKE', 'type' => 'DATE', ); $args['meta_query'][] = $event_published_date_query; } if (!empty($form_data['s'])) { $search_keyword = sanitize_text_field(strtolower($form_data['s'])); if ((isset($form_data['filter_by_eventTags']) || isset($form_data['filter_by_eventType'])) && empty($form_data['filter_by_eventYear'])) { $args['_meta_or_title'] = $search_keyword; } else { $args['s'] = $search_keyword; } } // Adding the taxonomy query to the main args $args['tax_query'] = $tax_query; } $postSearchQuery = new WP_Query($args); echo $postSearchQuery->request; if ($postSearchQuery->get('errors')) { $error_messages = $postSearchQuery->get('errors'); wp_send_json_error(array("errors" => $error_messages)); } return display_blog_posts($postSearchQuery, $paged); } Note: display_blog_posts() is the seperate function to display the posts in json format. Please help me to prepare the WP Args to fetch the posts in above such cases. I'll be very thankful to you. I also tried to modify the wp_query as well but it is also not worked. add_action('pre_get_posts', function ($q) { if ($keyword = $q->get('_meta_or_title')) { $allowed_post_types = array('events'); // Check if the current post type is allowed if (in_array($q->get('post_type'), $allowed_post_types)) { add_filter('get_meta_sql', function ($sql) use ($keyword) { global $wpdb; // Escape the keyword to prevent SQL injection $escaped_keyword = $wpdb->esc_like($keyword); // Modify WHERE part: $sql['where'] = sprintf( " AND ( (%s) OR (%s) OR (%s) OR %s ) ", $wpdb->prepare("{$wpdb->posts}.post_title LIKE %s", '%' . $escaped_keyword . '%'), $wpdb->prepare("{$wpdb->posts}.post_content LIKE %s", '%' . $escaped_keyword . '%'), $wpdb->prepare("(mt1.meta_key = '_post_custom_title' AND mt1.meta_value LIKE %s)", '%' . $escaped_keyword . '%'), mb_substr($sql['where'], 5, mb_strlen($sql['where'])) ); return $sql; }); } } }); Please help! Thanks in advance!
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.