Dark

Light

Dark

Light

Display WooCommerce products with a shortcode using a custom meta_query

This is normal that it doesn’t return anything as you need to pass this $args in a WP_Query first and to call a the product template in a loop this way:

if( ! function_exists('product_test') ) {

    // Add Shortcode
    function product_test( $atts ) {
        global $woocommerce_loop;

        // Attributes 
        $atts = shortcode_atts(
            array(
                'columns'   => '4',
                'limit'     => '20',
                'start'     => current_time('Ymd'),
                'end'       => current_time('Ymd'),
            ),
            $atts, 'products_test'
        );


        $woocommerce_loop['columns'] = $atts['columns'];

        // The WP_Query
        $products = new WP_Query( array (
            'post_type'         => 'product',
            'post_status'       => 'publish',
            'posts_per_page'    => $atts['limit'],
            'meta_query'        => array(
                'relation'      => 'AND',
                'start_clause'  => array(
                    'key'       =>'flash_sale_start',
                    'value'     => $atts['today'],
                    'compare'   => '<=',
                    'type'      => 'DATE'
                ),
                'end_clause'    => array(
                    'key'       => 'flash_sale_end',
                    'value'     => $atts['today'],
                    'compare'   => '>=',
                    'type'      => 'DATE'
                ),
            )
        ));

        ob_start();

        if ( $products->have_posts() ) { ?>

            <?php woocommerce_product_loop_start(); ?>

                <?php while ( $products->have_posts() ) : $products->the_post(); ?>

                    <?php wc_get_template_part( 'content', 'product' ); ?>

                <?php endwhile; // end of the loop. ?>

            <?php woocommerce_product_loop_end(); ?>

            <?php
        } else {
            do_action( "woocommerce_shortcode_products_loop_no_results", $atts );
            echo "<p>There is no results.</p>";
        }

        woocommerce_reset_loop();
        wp_reset_postdata();

        return '<div class="woocommerce columns-' . $atts['columns'] . '">' . ob_get_clean() . '</div>';
    }

    add_shortcode( 'products_test', 'product_test' );
}

Code goes in function.php file of the active child theme (or active theme).

USAGE:

There is 4 available optional arguments that you can add to this shortcode:

  • columns (The number of columns) – Default is 4
  • limit (the number of products | -1 will display all ) – Default is 20
  • start (Start date | format is ‘YMD’ ) – Default is today
  • end (End date | format is ‘YMD’ ) – Default is today

You can set any more in the function… You can change default values too.

Example 1 (simple with default values):

[products_test]

Example 2 (some custom values)

[products_test columns='3' limit='15']

Post a comment

Your email address will not be published.

40 comments

  1. DavidRon

    cheap ed pills from canada legal to buy prescription drugs without prescription
    cheap ed pills from india

  2. Anthonywaype

    over the counter prednisone pills: buy prednisone – iv prednisone

  3. Anthonywaype

    mail order prednisone: buy prednisone – prednisone online

  4. Anthonywaype

    prednisone tablet 100 mg: generic prednisone – prednisone pills for sale

  5. Albertlom

    http://zithromaxproff.com/# where to buy zithromax in canada
    zithromax 500 tablet

  6. Santossep

    https://zithromaxproff.com/# zithromax online pharmacy canada
    where can i get zithromax

  7. Albertlom

    http://zithromaxproff.com/# zithromax capsules
    zithromax online paypal

  8. Albertlom

    https://zithromaxproff.com/# zithromax 500 mg for sale
    zithromax online australia

  9. Raymondpab

    cleocin generic: biaxin online
    augmentin tablets

  10. BillyBom

    floxin tablets: nitrofurantoin for sale
    buy vantin generic

  11. Raymondpab

    zithromax for sale: buy biaxin generic
    order vantin online

  12. Raymondpab

    fucidin generic: roxithromycin price
    keflex online

  13. MichaelDiund

    united pharmacy india: best online international pharmacies india generic pills india

  14. WilliamGop

    order medications online from india: india pharmacy india pharmacies online

  15. MichaelDiund

    meds from india: prescriptions from india united pharmacy india

  16. MichaelDiund

    india pharmacy drugs: india pharmacy buy prescriptions from india pharmacy

  17. Robertcooto

    levitra pills: https://edpillsonline24.com/# over the counter ed medication

  18. DennisShalf

    levitra pills: viagra pills online viagra pills

  19. KeithCoumb

    over the counter viagra viagra prescription buying viagra online without prescription

  20. Michaelgox

    viagra from canada: non prescription viagra non prescription viagra

  21. Michaelgox

    canada viagra: viagra without a prescription viagra without a doctor prescription usa

  22. KeithCoumb

    buy generic 100mg viagra online no prescription viagra buying viagra online without prescription

  23. Claudaluck

    buy diflucan 150 mg: diflucan tablet price – diflucan buy without prescription

  24. Claudaluck

    how to order doxycycline: cheap doxycycline – doxycycline hyclate

  25. Claudaluck

    where to buy amoxicillin over the counter: over counter antibiotics pills amoxicillin – can you buy amoxicillin over the counter in canada

  26. Claudaluck

    clomiphene tablets: clomiphene citrate – clomid coupon

  27. Sosyal medya fenomeni olarak,
    En iyi instagram takipçi satış sitesinden Takipçi SAtın al
    hesabını uçuşa geçir

  28. Awesome! Its in fact awesome post, I have got much clear idea
    regarding from this post.

  29. If some one wants to be updated with most recent technologies therefore he must be
    go to see this web page and be up to date everyday.

  30. Hi there, this weekend is pleasant in support of me, because this moment i am reading
    this enormous informative article here at my home.

  31. Aktif gerçek İnstagram Takipçi Satın AL işlemini gerçekleştir!
    instagramaktiftakipci ile en hızlı takipçi satın al

  32. Greetings I am so glad I found your blog page, I really found you by accident, while
    I was browsing on Aol for something else, Anyhow I
    am here now and would just like to say many thanks for a tremendous post
    and a all round exciting blog (I also love the theme/design), I don’t have time to read it all at the moment but I have book-marked it and also included your RSS feeds, so when I have time I
    will be back to read a lot more, Please do keep up the fantastic work.

  33. Instagram aktif takipçi satın alarak siz de fenomen olabilirsiniz, fakat fenomen olmak isterken aynı zaman da bu işlemleri yapacağınız yerin kalitesi çok önemlidir bunu
    unutmayınız. Türk takipçi yazarak yabancı takipçi atılırsa hesabınıza bu sizin hesabınızın gerek görüntü gerek keşfete düşme gibi olasılıklarınızı tamamen bitirecektir.

    Bu yüzden aktif takipçi almanız çok önemli, aynı zamanda tabiki güvenilirlik ön planda olmalıdır.

    Biz uzman ekibimiz ile sizlere en iyi hizmeti vermek için sürekli çalışmaktayız.
    En iyi takipçi beğeni izlenme gibi sizleri üst seviyeye
    çıkarmak için elimizden geleni yaptık ve yapmaya devam
    edeceğiz. Ucuz Instagram takipçi için bizimle de iletişime geçebilirsiniz.
    Instagram bot takipçi içinde geçebilirsiniz fakat
    bunlara garanti vermemekteyiz, bot takipçilerin kesinlikle garantisi
    yoktur bir kaç gün içerisinde hepsi Instagram tarafından silinebilir.

    En aktif siteden hemen sende instagram aktif takipçi satın al

  34. Jasonnip

    lasix 100 mg: furosemide 20 mg tabs – lasix medication

  35. Keep this going please, great job!

test