hormigaz
hormigaz

Reputation: 167

How to modify shop page main query in WooCommerce to show a variation of every product in the list?

I'm doing a shoes shop in WooCommerce. Every shoe has a different color and size. I've added colors and sizes as attributes and then added some products as variable products.

I would like to list in the shop page all products, including only colors variations (not sizes!)... Example:

Then, my shop page should have four items:

The first step: I'm trying to target shop page main query to modify it and I can't! I'm doing like this:

function custom_pre_get_posts( $query ){
    // attempt #1
    if( ( 'product' == get_post_type() || is_post_type_archive('product') ) ):
        $query->set( 'posts_per_page', '1' );
        return;
    endif;

    // attempt #2
    if( is_shop() ):
        $query->set( 'posts_per_page', '1' );
        return;
    endif;
}
add_action('pre_get_posts', 'custom_pre_get_posts');

What am I doing wrong? Thanks in advance!

The second step: I will try to modify main query to target only the variations I've mentioned before... I do not have idea how to do that too... Any hints?

Thanks!!

Upvotes: 3

Views: 6666

Answers (1)

Worked for me

// ALTERAR A QUERY PRINCIPAL
function fwp_archive_per_page( $query ) {
    if ( is_tax( 'product_cat' ) || is_post_type_archive('product') ) {
        
        $query->set( 'showposts', 16 );
        $query->set( 'posts_per_page', 16 );
        $query->set( 'meta_query', array(
                                      'relation'    => 'AND',
                                    array(
                                        'key'   => 'condicoes_de_exibicao',
                                        'value'     => array('Não exibir no site', 'Não exibir em lugar nenhum'),
                                        'compare'   => 'NOT IN',
                                      )
                                    ) );

    }
}
add_filter( 'pre_get_posts', 'fwp_archive_per_page' );

Upvotes: 5

Related Questions