Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Longkt/a77f4fb34f87f7eb449d75b073f9e206 to your computer and use it in GitHub Desktop.
Save Longkt/a77f4fb34f87f7eb449d75b073f9e206 to your computer and use it in GitHub Desktop.
// Sort by Popularity default in Category coupon page
add_action( 'init', function() {
remove_action( 'wpcoupon_coupon_category_before_sidebar', 'wpcoupon_coupon_cat_filter_box', 10 );
remove_filter( 'pre_get_posts', 'wpcoupon_coupon_cat_query' );
add_action( 'wpcoupon_coupon_category_before_sidebar', 'custom_wpcoupon_coupon_cat_filter_box', 10 );
add_filter( 'pre_get_posts', 'custom_wpcoupon_coupon_cat_query' );
} );
/**
* Custom query for custom tax
*
* @todo: fix page not found on coupon page tax
*
* @param $query
* @return mixed
*/
function custom_wpcoupon_coupon_cat_query( $query ) {
if ( get_query_var( 'taxonomy' ) == 'coupon_category' && $query->is_main_query() ) {
$cate_id = get_queried_object_id();
$coupon_type = 'all';
$available_coupon_type = wpcoupon_get_coupon_types();
$all_coupon_in_cat = wpcoupon_get_all_coupon_from_cat( $cate_id );
$get_coupon_var = ( isset( $_GET['coupon_type'] ) ) ? sanitize_text_field( wp_unslash( $_GET['coupon_type'] ) ) : '';
$filtered_sortby = ( isset( $_GET['sort_by'] ) ) ? sanitize_text_field( wp_unslash( $_GET['sort_by'] ) ) : 'newest';
$number_active = intval( wpcoupon_get_option( 'coupon_cate_number', 15 ) );
$query->set( 'posts_per_page', $number_active );
if ( ( ! isset( $_GET['coupon_type'] ) && ! isset( $_GET['sort_by'] ) ) || ( ( '' == $get_coupon_var || 'all' == $get_coupon_var ) && 'newest' == $filtered_sortby ) ) {
if ( isset( $all_coupon_in_cat['not_expired'] ) && isset( $all_coupon_in_cat['expired'] ) ) {
$post_in = array_merge( $all_coupon_in_cat['not_expired'], $all_coupon_in_cat['expired'] );
$query->set( 'post__in', $post_in );
$query->set( 'orderby', 'post__in' );
if( ! isset( $_GET['sort_by'] ) ) {
$query->set( 'meta_key', '_wpc_used' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'desc' );
$query->set( 'meta_type', 'NUMERIC' );
}
}
} else {
if ( isset( $get_coupon_var ) && array_key_exists( $get_coupon_var, $available_coupon_type ) ) {
$coupon_type = $get_coupon_var;
}
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query->set( 'paged', $paged );
$now = current_time( 'timestamp' );
$meta_query = array();
if ( 'all' != $coupon_type ) {
$meta_query = array(
'relation' => 'AND',
array(
'key' => '_wpc_coupon_type',
'value' => $coupon_type,
'compare' => '=',
),
);
}
switch ( $filtered_sortby ) {
case 'popularity':
$query->set( 'meta_key', '_wpc_used' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'desc' );
$query->set( 'meta_type', 'NUMERIC' );
break;
case 'ending-soon':
$query->set( 'meta_key', '_wpc_expires' );
$query->set( 'meta_value', $now );
$query->set( 'meta_compare', '>=' );
$query->set( 'meta_type', 'NUMERIC' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'asc' );
break;
case 'expired':
$meta_query[] = array(
array(
'key' => '_wpc_expires',
'compare' => 'EXISTS',
),
array(
'key' => '_wpc_expires',
'type' => 'NUMERIC',
'value' => 0,
'compare' => '>',
),
array(
'key' => '_wpc_expires',
'value' => $now,
'type' => 'NUMERIC',
'compare' => '<=',
),
);
$query->set( 'meta_key', '_wpc_expires' );
$query->set( 'orderby', 'meta_value' );
$query->set( 'meta_type', 'NUMERIC' );
$query->set( 'order', 'desc' );
break;
default:
$query->set( 'orderby', 'menu_order date' );
$query->set( 'order', 'desc' );
break;
}
if ( ! empty( $meta_query ) ) {
$query->set( 'meta_query', $meta_query );
}
}
}
return $query;
}
add_filter( 'pre_get_posts', 'wpcoupon_coupon_cat_query' );
/**
* Render filter box for category
*
* @since 1.2.6
*/
function custom_wpcoupon_coupon_cat_filter_box() {
$is_enable_filter = wpcoupon_get_option( 'coupon_cate_sidebar_filter', true );
$title = wpcoupon_get_option( 'coupon_cate_filter_title', 'Filter' );
if ( ! $is_enable_filter ) {
return;
}
$filtered_sortby = ( isset( $_GET['sort_by'] ) ) ? sanitize_text_field( wp_unslash( $_GET['sort_by'] ) ) : 'popularity';
?>
<aside class="widget widget_coupon_cat_filter">
<h4 class="widget-title"><?php echo wp_kses_post( $title ); ?></h4>
<div class="shadow-box">
<?php
$current_url = wpcoupon_current_url();
$filter_url = add_query_arg( array( 'coupon_type' => '' ), $current_url );
$base_pagenum = get_pagenum_link( 1 );
global $wp_query;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$max_pages = $wp_query->max_num_pages;
if ( $max_pages >= ( $paged + 1 ) ) {
$next_page = ( $paged + 1 );
} else {
$next_page = $paged;
}
?>
<input type="hidden" class="store_base_pagenum_url" name="store_base_pagenum_url" value="<?php echo get_pagenum_link( 1, false ); ?>"/>
<input type="hidden" class="store_pagenum_url" name="store_pagenum_url" value="<?php echo get_pagenum_link( $paged ); ?>"/>
<input type="hidden" class="store_next_pagenum_url" name="store_next_pagenum_url" value="<?php echo get_pagenum_link( $next_page ); ?>"/>
<div class="couponcat-sortby-wrapper ui list">
<h5><?php echo esc_html_e( 'Sort by', 'wp-coupon' ); ?></h5>
<div class="item">
<label for="store-sortby-newest">
<input id="store-sortby-newest" <?php checked( 'newest', $filtered_sortby ); ?> type="radio" class="coupon-cat-sortby sortby-newest" name="coupon-cat-sortby" value="newest" />
<span class="filter-sortby-name"><?php echo esc_html_e( 'Newest', 'wp-coupon' ); ?></span>
</label>
</div>
<div class="item">
<label for="store-sortby-popularity">
<input id="store-sortby-popularity" <?php checked( 'popularity', $filtered_sortby ); ?> type="radio" class="coupon-cat-sortby sortby-popularity" name="coupon-cat-sortby" value="popularity" />
<span class="filter-sortby-name"><?php echo esc_html_e( 'Popularity', 'wp-coupon' ); ?></span>
</label>
</div>
<div class="item">
<label for="store-sortby-endingsoon">
<input id="store-sortby-endingsoon" <?php checked( 'ending-soon', $filtered_sortby ); ?> type="radio" class="coupon-cat-sortby sortby-ending-soon" name="coupon-cat-sortby" value="ending-soon" />
<span class="filter-sortby-name"><?php echo esc_html_e( 'Ending Soon', 'wp-coupon' ); ?></span>
</label>
</div>
<div class="item">
<label for="store-sortby-expired">
<input id="store-sortby-expired" <?php checked( 'expired', $filtered_sortby ); ?> type="radio" class="coupon-cat-sortby sortby-expired" name="coupon-cat-sortby" value="expired" />
<span class="filter-sortby-name"><?php echo esc_html_e( 'Expired', 'wp-coupon' ); ?></span>
</label>
</div>
</div>
</div>
</aside>
<?php
}
add_action( 'wpcoupon_coupon_category_before_sidebar', 'wpcoupon_coupon_cat_filter_box', 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment