I hope this is not too specific to WooCommerce.

I have a nifty shortcode that displays a list of all my products with SKUs. However, it also includes products that I have published but have set the catalog visibility to “hidden.”

I can’t find an argument / parameter to exclude hidden products (or only include those marked as Catalog/Search).

I know it must be simple; I just haven’t found it. Thanks for any help.

Here is the code:

<?php
$params = array('posts_per_page' => -1, 'post_type' => 'product', 'orderby' => 'menu-order', 'order' => 'asc');
$wc_query = new WP_Query($params);
?>
<table class="product-list tablesorter"><thead><tr><th>SKU</th><th>Product Name</th></tr></thead><tbody>
     <?php if ($wc_query->have_posts()) : ?>
     <?php while ($wc_query->have_posts()) :
                $wc_query->the_post(); ?>
 <tr>
<td><?php global $product; echo $product->get_sku(); ?></td>
<td><a href="https://wordpress.stackexchange.com/questions/231118/<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></td>
</tr>
     <?php endwhile; ?>
     <?php wp_reset_postdata(); ?>
     <?php else:  ?>
     <tr><td>
          <?php _e( 'No Products' ); ?>
    </td> </tr>
     <?php endif; ?>
</tbody>
</table>

Best Answer

Important: The following only works for WooCommerce versions less than 3.0. For a more up-to-date answer please see the other answer by kalle.

WooCommerce save this data as metadata so you’ll need to run a Meta Query against the name _visibility. Something like:

'meta_query' => array(
    array(
        'key'       => '_visibility',
        'value'     => 'hidden',
        'compare'   => '!=',
    )
)

This will pull all posts that do not have meta _visibility equal to hidden.

Leave a Reply

Your email address will not be published. Required fields are marked *