Question

At some point, the "Featured" images that usually showed up on our homepage have stopped showing.

I've done some digging and found this template code in index.php (this is the Artificer theme by WooThemes)

This seems to be the code that handles the featured images:

<ul class="featured-products">

<!-- The first 3 -->
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 3, 'meta_query' => array( array('key' => '_visibility','value' => array('catalog', 'visible'),'compare' => 'IN'),array('key' => '_featured','value' => 'yes')) );
$loop = new WP_Query( $args );

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

if ( function_exists( 'get_product' ) ) {
    $_product = get_product( $loop->post->ID );
} else {
    $_product = new WC_Product( $loop->post->ID );
}

?><li class="featured">

            <?php //woocommerce_show_product_sale_flash( $post, $_product ); ?>
            <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
                <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" />'; ?>


            <h3><?php the_title(); ?> <span class="price"><?php echo $_product->get_price_html(); ?></span></h3>

            </a>

    </li><?php endwhile; ?><!-- the large 1 --><?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'offset' => 3, 'meta_query' => array( array('key' => '_visibility','value' => array('catalog', 'visible'),'compare' => 'IN'),array('key' => '_featured','value' => 'yes')) );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
if ( function_exists( 'get_product' ) ) {
    $_product = get_product( $loop->post->ID );
} else {
    $_product = new WC_Product( $loop->post->ID );
}
?><li class="featured">

            <?php //woocommerce_show_product_sale_flash( $post, $_product ); ?>
            <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
                <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_single'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" />'; ?>


            <h3><?php the_title(); ?> <span class="price"><?php echo $_product->get_price_html(); ?></span></h3>

            </a>

        </li><?php endwhile; ?><!-- the last 3 --><?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 3, 'offset' => 4, 'meta_query' => array( array('key' => '_visibility','value' => array('catalog', 'visible'),'compare' => 'IN'),array('key' => '_featured','value' => 'yes')) );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); $_product;
if ( function_exists( 'get_product' ) ) {
    $_product = get_product( $loop->post->ID );
} else {
    $_product = new WC_Product( $loop->post->ID );
}
?><li class="featured">

            <?php //woocommerce_show_product_sale_flash( $post, $_product ); ?>
            <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
                <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" />'; ?>


            <h3><?php the_title(); ?> <span class="price"><?php echo $_product->get_price_html(); ?></span></h3>

            </a>

        </li><?php endwhile; ?>
</ul>

<?php } ?>

I found out how to grab the last run SQL query, and it comes back as:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts 
    INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) 
    INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) 
    WHERE 1=1 
    AND ( 
        ( wp_postmeta.meta_key = '_visibility' AND wp_postmeta.meta_value IN ('catalog','visible') ) 
        AND ( mt1.meta_key = '_featured' AND mt1.meta_value = 'yes' ) ) 
        AND wp_posts.post_type = 'product' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') 
        GROUP BY wp_posts.ID 
        ORDER BY wp_posts.post_date DESC 
        LIMIT 0, 3

Sure enough, if I run that in phpMyAdmin I get an ermpty result set. Why would this suddently happen? We did upgrade to WooCommerce 3.0.5 a couple of weeks back, so I'm wondering if maybe something internally has changed that needs updating?

Update: Digging into this a bit more, I found that this doesn't seem to be getting set:

( mt1.meta_key = '_featured' AND mt1.meta_value = 'yes' ) ) 

They are showing up as "featured" in the WooCommerce admin area though:

enter image description here

How does WooCommerce decide if a post is featured? My understanding from the code in question above, is that it is doing:

wp_postmeta.meta_key = "_featured"
AND 
wp_postmeta.meta_value = "yes"

...but that can't be right, as literally no values in the wp_postmeta table even match %featured% !

UPDATE 3: As suggested, I turned on debugging in WP, and I now get this on the homepage:

Notice: Undefined index: woo_enable_lightbox in /home/costumes/web/thecostumerooms.co.uk/public_html/test/wp-content/themes/artificer/includes/theme-woocommerce.php on line 274
Notice: Undefined index: woo_shop_archives_fullwidth in /home/costumes/web/thecostumerooms.co.uk/public_html/test/wp-content/themes/artificer/includes/theme-woocommerce.php on line 91
class="home blog logged-in admin-bar no-customize-support gecko alt-style-default two-col-left "> 

Notice: Undefined index: woo_display_store_info in /home/costumes/web/thecostumerooms.co.uk/public_html/test/wp-content/themes/artificer/index.php on line 141

Notice: Undefined index: woo_display_store_info in /home/costumes/web/thecostumerooms.co.uk/public_html/test/wp-content/themes/artificer/index.php on line 144

Notice: Undefined index: woo_display_store_info in /home/costumes/web/thecostumerooms.co.uk/public_html/test/wp-content/themes/artificer/index.php on line 167

That is on a vanilla install of the latest WP, along with Artificer and WooCommerce (3.0.5)

Was it helpful?

Solution

These are WooCommerce 3.x instructions only.

That thread on GitHub (https://github.com/woocommerce/woocommerce/issues/14914#issuecomment-299714488) was updated by one of the Woo devs and he provided an example loop and explained what is happening:

Featured was meta in 2.6, but is a taxonomy (for performance!) in 3.0 - so adjust queries to use tax_query instead of meta_query accordingly.

To apply the new args to your Artificer theme by WooThemes (too bad they didn't update the theme too), do the following:

For Non-developers:

Use FTP to login to your host. There are many ftp client apps, type in "ftp clients" into Google w/o quotes. Then pick one for your OS. Login with your credentials that you got when you purchased your hosting. Generally, these are the same as your cPanel login username and password. You would enter ftp.yourdomain.com. You will be in to your public_html directory. Then locate your wp_content directory, in that directory you'll see more sub-directories, one of which is themes inside that you'll see the directory name artificer. The file that is the home page is index.php. That is the file that you swap with the one in this gist.

https://gist.github.com/carasmo/03cbbd2f70c30d0649de4ada7095a3dd

Click on the button "raw" then save (to a directory you create for this on your computer) as index.php. Don't open it unless you have a code editor. Then drag it from your computer to the ../wp-content/themes/artificer directory.

For devs:

Replace two locations where the loop $args are:

loop 1 for three images:

        $args = array(
            'post_type' => 'product',
            'posts_per_page' => 3,
            'tax_query' => array(
                    array(
                        'taxonomy' => 'product_visibility',
                        'field'    => 'name',
                        'terms'    => 'featured',
                    ),
                ),
            );

loop 2 for the single image

        $args = array(
            'post_type' => 'product',
            'posts_per_page' => 1,
            'offset' => 3,
            'tax_query' => array(
                    array(
                        'taxonomy' => 'product_visibility',
                        'field'    => 'name',
                        'terms'    => 'featured',
                    ),
                ),
            );
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top