wpgenie

Custom shortcode to display active auctions (without future and past ones), usage [number_of_active_auctions]

Nov 11th, 2020 (edited)
1,498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.08 KB | None | 0 0
  1. /*
  2. Custom shortcode to display active auctions (without future and past ones), usage [number_of_active_auctions]  
  3. Code should be copy pasted to your child theme functions.php file.
  4. */
  5.  
  6. add_shortcode('number_of_active_auctions', 'number_of_active_auctions_function');
  7. function number_of_active_auctions_function( ) {
  8.    
  9.     global $woocommerce;
  10.     $meta_query = $woocommerce->query->get_meta_query();
  11.    
  12.     $args = array(
  13.         'post_type' => 'product',
  14.         'post_status' => 'publish',
  15.         'ignore_sticky_posts'   => 1,
  16.         'meta_query' => array(
  17.             'relation' => 'AND',
  18.             // don't include past auctions
  19.             'past_auctions' => array(
  20.                     'meta_key'     => '_auction_closed',
  21.                     'meta_compare' => 'NOT EXISTS'),
  22.             // don't include future auctions
  23.             'future_auctions' => array(
  24.                     'meta_key'     => '_auction_started',
  25.                     'meta_compare' => 'EXISTS')
  26.         ),
  27.         'tax_query' => array(array('taxonomy' => 'product_type' , 'field' => 'slug', 'terms' => 'auction'))
  28.        
  29.     );
  30.     $products = new WP_Query( $args );
  31.     echo "Number of active auctions: ".$products->found_posts;
  32.     wp_reset_postdata();   
  33. }
Advertisement
Add Comment
Please, Sign In to add comment