0

I'm trying to set a product tag archive page as the front page.

Using this code:

function set_product_tag_archive_as_front_page( $query ) {

    // Only filter the main query on the front-end
    if ( is_admin() || ! $query->is_main_query() ) {
        return;
    }

    global $wp;
    $front = false;

    // If the latest posts are showing on the home page
    if ( ( is_home() && empty( $wp->query_string ) ) ) {
        $front = true;
    }

    // If a static page is set as the home page
    if ( ( $query->get( 'page_id' ) == get_option( 'page_on_front' ) && get_option( 'page_on_front' ) ) || empty( $wp->query_string ) ) {
        $front = true;
    }

    if ( $front ) :

        $query->set( 'post_type', 'product' );
        $query->set( 'product_tag', 'hobbies' );
        $query->set( 'page_id', '' );

        // Set properties to match an archive
        $query->is_page = 0;
        $query->is_singular = 0;
        $query->is_product_tag = 1;
        $query->is_shop = 1;
        $query->is_archive = 1;
        $query->is_tax = 1;
        $query->is_product = 1;
        $query->is_woocommerce = 1;

    endif;

}
add_action( 'pre_get_posts', 'set_product_tag_archive_as_front_page' );

I managed to get layout all correct as the original archive page - but no products are showing.

If I comment-out $query->is_singular = 0; the products are showing, but the layout is wrong.

This is the front page as it is now: http://simplyhobbies.life/

This how it suppose to be: http://simplyhobbies.life/product-tag/hobbies/

What am I missing?

Thanks, Asaf

  • This is a really interesting way of going about this. Based on your scenario it sounds like Woocommerce is changing templates and determining display based on some of the query conditions which is messy. Are you able to modify your theme templates and/or theme settings to alter the front page? If so, that's the way I would do it. If that's a possibility, I'll provide a more detailed answer. – MikeNGarrett Mar 27 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.