Not able to get woocommerce featured Product

I am trying to display 6 featured products with their thumbnails, price and title. But I am not able to see anything, however if I use $loop->found_posts I can see there are 6 records fetching back from database.
I have also added these lines in wp-config.php to see the errors but I am not able to see any error on the page

wp-config.php

Read More
define('WP_DEBUG', true);
if (WP_DEBUG) {
    define('WP_DEBUG_LOG', true);
    define('WP_DEBUG_DISPLAY', true);
    @ini_set('display_errors', 0);
}

here is my code for displaying featured posts

<?php
    $args = array (
                    'post_type'=>'product',
                    'meta_key'=>'_featured',
    'posts_per_page'=>6
    );
    $loop= new WP_Query($args);
    //echo $loop->found_posts;
    while($loop->have_posts()): the_post();
    echo '
    <div class="col-xs-4">
    <div class="custom-product">
        <img src="'.woocommerce_get_product_thumbnail(300,335).'">
        <div class="price-title">
            <h2>'.the_title().'</h2>
            <h3>'.$product->get_price_html().'</h3>
        </div>
    </div>
    </div>
    ';
    endwhile;
?>

Related posts

Leave a Reply

4 comments

  1. Few suggestions here:

    • You should use:

      while( $loop->have_posts() ): $loop->the_post();
      

      instead of:

      while( $loop->have_posts() ): the_post();
      

      because otherwise you are setting up posts related to the global $wp_query object.

    • Note that the_title() doesn’t return the value, it is echo-ing it.

    • Make sure $product is defined in your loop.

    • Use wp_reset_postdata() after your secondary query, to restore the global $post variable.

    • You can use the posts property of WP_Query:

      var_dump( $loop->posts );
      

      to peek into the array of queried posts.

    • To check the generated SQL query, we can use the request property of WP_Query:

      echo $loop->request
      

      This can come handy when we need to debug our query.

  2. This should work as expected. woocommerce_get_product_thumbnail echoes the image tag HTML, what you need to pass as parameter is an existing thumbnail name, and width and height of the placeholder.

    <?php
        $args = array(
            'post_type' => 'product',
            'meta_key' => '_featured',
            'posts_per_page' => 6
        );
    
        $loop = new WP_Query( $args );
        global $product;
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <div class="col-xs-4">
                <div class="custom-product">
                    <?php echo woocommerce_get_product_thumbnail( 'shop_catalog', 300, 335 ); ?>
                    <div class="price-title">
                        <h2><?php the_title(); ?></h2>
                        <h3><?php echo $product->get_price_html(); ?></h3>
                    </div>
                </div>
            </div>
    <?php 
        endwhile;
        wp_reset_postdata();
    ?>
    

    You can set custom image dimensions as stated here -> WooCommerce Codex

  3. shop_catalog My first hit on google returned the following link.

    Based on their instructions i updated your code.
    The following code should work:

    <?php
        $args = array (
                        'post_type' => 'product',
                        'meta_key' => '_featured',
                        'meta_value' => 'yes', 
                        'posts_per_page' => 6
        );
        global $post;
        $loop= new WP_Query($args);
        //echo $loop->found_posts;
        while($loop->have_posts()): $loop->the_post();
        $post = $loop->post;
        setup_postdata( $post );
        $product = get_product( $loop->post->ID );
        echo '
        <div class="col-xs-4">
        <div class="custom-product">
            <img src="' . woocommerce_get_product_thumbnail('shop_catalog ', 300,335) . '">
            <div class="price-title">
                <h2>' . get_the_title(). '</h2>
                <h3>' . $product->get_price_html() . '</h3>
            </div>
        </div>
        </div>
        ';
        endwhile;
        wp_reset_postdata();
    ?>
    
  4. I got the same problem. Try this ! Works for me

     <?php
     $featured_query = new WP_Query( array(
         'tax_query' => array(
                 array(
                     'taxonomy' => 'product_visibility',
                     'field'    => 'name',
                     'terms'    => 'featured',
                     'operator' => 'IN'
                 ),
          ),
     ) );
     ?>