// Override template for event categories add_filter('template_include', 'sn_override_event_category_template', 99); function sn_override_event_category_template(\$template) { // Only override for product category archives if (is_tax('product_cat')) { \$term = get_queried_object(); if (\$term) { // Check if this category has events \$test_query = new WP_Query(array( 'post_type' => 'product', 'posts_per_page' => 1, 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => \$term->slug, ), ), 'meta_query' => array( array( 'key' => 'WooCommerceEventsEvent', 'value' => 'Event', 'compare' => '=' ) ) )); if (\$test_query->have_posts()) { \$custom_template = get_template_directory() . '/taxonomy-product_cat.php'; if (file_exists(\$custom_template)) { wp_reset_postdata(); return \$custom_template; } } wp_reset_postdata(); } } return \$template; } // Style event category pages add_action('wp_head', 'sn_style_event_category_pages', 999); function sn_style_event_category_pages() { if (is_tax('product_cat')) { \$term = get_queried_object(); if (\$term) { // Check if this category has events \$test_query = new WP_Query(array( 'post_type' => 'product', 'posts_per_page' => 1, 'tax_query' => array( array('taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => \$term->slug) ), 'meta_query' => array( array('key' => 'WooCommerceEventsEvent', 'value' => 'Event', 'compare' => '=') ) )); if (\$test_query->have_posts()) { wp_reset_postdata(); echo ''; } wp_reset_postdata(); } } } // Modify query to sort events by date (upcoming first, then past) add_action('pre_get_posts', 'sn_sort_event_category_products', 20); function sn_sort_event_category_products(\$query) { if (!is_admin() && \$query->is_main_query() && is_tax('product_cat')) { \$term = get_queried_object(); if (\$term) { // Check if this category has events \$test_query = new WP_Query(array( 'post_type' => 'product', 'posts_per_page' => 1, 'tax_query' => array( array('taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => \$term->slug) ), 'meta_query' => array( array('key' => 'WooCommerceEventsEvent', 'value' => 'Event', 'compare' => '=') ) )); if (\$test_query->have_posts()) { wp_reset_postdata(); // Sort by event date - upcoming first (ASC), then past (DESC) // We'll use a custom orderby that puts future dates first \$current_timestamp = current_time('timestamp'); \$query->set('orderby', 'meta_value_num'); \$query->set('meta_key', 'WooCommerceEventsDateTimestamp'); // Sort ascending so nearest upcoming events come first \$query->set('order', 'ASC'); \$query->set('meta_query', array( array( 'key' => 'WooCommerceEventsEvent', 'value' => 'Event', 'compare' => '=' ) )); } } } } // Use posts_orderby filter to properly sort: upcoming first (ASC), then past (DESC) add_filter('posts_orderby', 'sn_custom_event_orderby', 10, 2); function sn_custom_event_orderby(\$orderby, \$query) { if (!is_admin() && \$query->is_main_query() && is_tax('product_cat')) { \$term = get_queried_object(); if (\$term) { global \$wpdb; \$current_timestamp = current_time('timestamp'); // Custom SQL to sort: upcoming events first (ASC), then past events (DESC) \$orderby = "CASE WHEN CAST(\$wpdb->postmeta.meta_value AS UNSIGNED) >= \$current_timestamp THEN CAST(\$wpdb->postmeta.meta_value AS UNSIGNED) ELSE 999999999999 END ASC, CASE WHEN CAST(\$wpdb->postmeta.meta_value AS UNSIGNED) < \$current_timestamp THEN CAST(\$wpdb->postmeta.meta_value AS UNSIGNED) ELSE 0 END DESC"; } } return \$orderby; } Delhi NCR – StartupNews.fyi – Startup & Technology News
Advertisement
Connect with us
Welcome to Startup News - Your source for the latest startup events, news, and insights