WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Custom Product Loop - Exclude "Hidden" Products (5 posts)

  1. nickzee
    Member
    Posted 1 year ago #

    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 http://demo.mintthemes.com/soundstage/

    Thank you very much.
    Nick

    http://wordpress.org/extend/plugins/woocommerce/

  2. Yojance
    Member
    Posted 1 year ago #

  3. jameskoster
    Member
    Plugin Author

    Posted 1 year ago #

    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'
    )
    )
  4. nickzee
    Member
    Posted 1 year ago #

    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

  5. Yojance
    Member
    Posted 9 months ago #

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic