wpgenie icon

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

wpgenie | PRO | 11/12/20 08:21:44 AM UTC (Edited) | 0 ⭐ | 1925 👁️ | Never ⏰ | []
PHP |

1.08 KB

|

None

|

0 👍

/

0 👎

/* 
Custom shortcode to display active auctions (without future and past ones), usage [number_of_active_auctions]  
Code should be copy pasted to your child theme functions.php file.
*/
 
add_shortcode('number_of_active_auctions', 'number_of_active_auctions_function');
function number_of_active_auctions_function( ) {
    
    global $woocommerce;
    $meta_query = $woocommerce->query->get_meta_query();
    
    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'ignore_sticky_posts'   => 1,
        'meta_query' => array(
            'relation' => 'AND',
            // don't include past auctions
            'past_auctions' => array(
                    'meta_key'     => '_auction_closed',
                    'meta_compare' => 'NOT EXISTS'),
            // don't include future auctions
            'future_auctions' => array(
                    'meta_key'     => '_auction_started',
                    'meta_compare' => 'EXISTS')
        ),
        'tax_query' => array(array('taxonomy' => 'product_type' , 'field' => 'slug', 'terms' => 'auction'))
        
    );
    $products = new WP_Query( $args );
    echo "Number of active auctions: ".$products->found_posts;
    wp_reset_postdata();    
}

Comments