• Hello.

    I’m using a theme that has a custom product loop on the home page. Everything is great except “hidden” and/or “private” products are showing on the home page. Can someone please look at this code and let me know what to add in order to exclude “hidden” products.

    <?php if (of_get_option("cap_show_merch") == true){?>
    		<?php //woocommerce check ?>
    		<?php if (  in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { ?>
    			<h2><?php echo of_get_option("cap_merch_title"); ?></h2>
    			<div class="preview-block">
    				<?php
                    global $product;
                    $num = 0;
                    $merch = new WP_Query( array(
                    'posts_per_page' => 5,
                    'post_type'      => array( 'product' )
                    ) );
    
                    if( $merch->have_posts() ) : while( $merch->have_posts() ) : $merch->the_post();
                        $product = get_product();
    
                        if ( $num == 0 ) : ?>
                            <div class="preview">
                                <div class="visual">
                                    <?php if ( has_post_thumbnail() ) : ?>
                                        <img src="<?php echo aq_resize( wp_get_attachment_url( get_post_thumbnail_id(), 'full' ), 473, 453, true );  ?>" width="473" height="453" alt="<?php the_title(); ?>" />
                                    <?php else : ?>
                                        <img src="<?php echo woocommerce_placeholder_img_src(); ?>" alt="Placeholder" />
                                    <?php endif; ?>
                                    <a href="<?php the_permalink(); ?>" class="mask"></a>
                                    <?php woocommerce_template_loop_price(); ?>
                                </div>
                                <a href="<?php the_permalink(); ?>" class="caption"><?php the_title(); ?></a>
                            </div>
                        <?php else : ?>
                            <?php if ( $num == 1 ) : ?>
                                <ul class="catalog">
                            <?php endif; ?>
                            <li>
                                <div class="visual">
                                    <?php if ( has_post_thumbnail() ) : ?>
                                        <img src="<?php echo aq_resize( wp_get_attachment_url( get_post_thumbnail_id(), 'full' ), 222, 193, true );  ?>" width="222" height="193" alt="<?php the_title(); ?>" />
                                    <?php else : ?>
                                        <img src="<?php echo woocommerce_placeholder_img_src(); ?>" alt="Placeholder" />
                                    <?php endif; ?>
                                    <a href="<?php the_permalink(); ?>" class="mask"></a>
                                    <?php woocommerce_template_loop_price(); ?>
                                </div>
                                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                            </li>
                            <?php
                        endif;
                        $num++;
                    endwhile;
                    ?>
                    </ul>
                    <?php endif; wp_reset_postdata(); ?>
    			</div><!--preview-block-->
    		<?php } //woocommerce check?>
    	<?php } ?>

    This is the theme https://demo.mintthemes.com/soundstage/

    Thank you very much.
    Nick

    https://www.ads-software.com/extend/plugins/woocommerce/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Try changing it to this:

    https://pastebin.com/hMwstqs9

    I added ‘post_status’ => ‘publish’

    https://codex.www.ads-software.com/Class_Reference/WP_Query#Status_Parameters

    Plugin Contributor James Koster

    (@jameskoster)

    Hidden products are still published.

    You need to add a meta query to check the visibility;

    'meta_query' => array(
    array(
    'key' => '_visibility',
    'value' => array( 'catalog', 'visible' ),
    'compare' => 'IN'
    )
    )
    Thread Starter nickzee

    (@nickzee)

    Thank you Gentleman.

    I have been working on adding this over 2 hours. I copied and pasted James’s code like this,

    <?php
                    global $product;
                    $num = 0;
                    $merch = new WP_Query( array(
                    'posts_per_page' => 5,
                    'post_type'      => array( 'product' )
                    'meta_query' => array(
    array(
    'key' => '_visibility',
    'value' => array( 'catalog', 'visible' ),
    'compare' => 'IN'
    )
    )
    				) );

    But this returns an error,

    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ‘)’

    I’ve used syntax checkers and researched the meta_query array without any luck as to making this work. If any one has the time, I would appreciate further guidance.

    Thank you

    I know this is old but I can see there is a missing , after
    'post_type' => array( 'product' )

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom Product Loop – Exclude "Hidden" Products’ is closed to new replies.