Using the post expirator plugin see useful plugins
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function my_custom_posts_query( $query, $args ){ if( $args['query'] == 'expires_first' ){ $query = array( 'cat' => 1, 'order' => 'ASC', 'meta_key' => '_expiration-date', 'orderby' => 'meta_value' ); if(is_front_page())//optional $query['posts_per_page'] = 5; } return $query; } add_filter( 'themeblvd_posts_args', 'my_custom_posts_query', 10, 2 ); |
For woo products with variable category
query like so;
query=”expires_first|catid=248″
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | function jma_expire_products_query( $query, $args ){ if (0 === strpos($args['query'], 'expires_first')){ $exploded = explode('|', $args['query']); $query = array( 'post_type' => 'product', 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'id', 'terms' => str_replace('catid=', '', $exploded[1]), ), ), 'order' => 'ASC', 'meta_key' => '_expiration-date', 'orderby' => 'meta_value' ); } return $query; } add_filter( 'themeblvd_posts_args', 'jma_expire_products_query', 10, 2 ); |
using pre_get_posts:
0 1 2 3 4 5 6 7 8 9 | function date_search_filter($query) { if (! is_admin() && $query->is_main_query()&& $query->query_vars['category_name'] == 'events') { $query->set('meta_key', '_expiration-date'); $query->set('orderby', array('meta_value' => 'ASC')); } } add_action('pre_get_posts', 'date_search_filter'); |