Nick Zimmer
Nick Zimmer

Reputation: 41

Woocommerce Meta_Query for Hidden Products

I'm using a theme that has a custom product loop on the home page. Everything is great except "hidden" products are showing on the home page. I was provide a bit of code to fix it, but keep getting Parse errors.

Current Code from index.php:

<?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 } ?>

I asked this in the Wordpress forums here, and was provided this code my James Koster.

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

When I added it to the php under "'post_type' => array( 'product' )" it looks 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 ')' in/.../themes/destinlive/index.php on line 116".

Line 116 is "'meta_query' => array("

I'm not at all proficient in php, and the lack of detail from James as to where to put it, (I'm thankful for the help) has left me spending several hours trying different configurations but with no avail. Now I'm turning here for help.

Thank you in advance for your generous help.

Upvotes: 4

Views: 2257

Answers (1)

Jules Colle
Jules Colle

Reputation: 11939

You're missing a comma after 'post_type' => array( 'product' )

Upvotes: 4

Related Questions