• Resolved CharlieCoplestone

    (@charliecoplestone)


    I’m trying to essentially filter Woocommerce products by looping through the terms and allowing the user to click the permalink which will take them to a page which filters the products based on what was clicked:

        if ( is_array( $brands ) ) {
            echo '<div class="filters__brand">
                    <div class="filters__tab" data-filter-toggle="">
                        <span class="filters__iden">Brands</span>
                        <span class="filters__close"></span>
                    </div>
                    <div class="filters__itemwrapper">';
            foreach ( $brands as $brand ){
                echo '<div><a style="color:black" href="' . htmlentities( add_query_arg( 'pwb-brand', $brand->slug, $page_link ) ) . '">' . $brand->name . '</a></div>';
            }
            echo '<li><a href="' . htmlentities( remove_query_arg( 'pwb-brand', $page_link ) ) . '">None</a></li>';
            echo '</div> </div>';
        }
        
        if ( is_array( $categories ) ) {
            echo '<div class="filters__cat">
                    <div class="filters__tab" data-filter-toggle="">
                        <span class="filters__iden">Type</span>
                        <span class="filters__close"></span>
                    </div>
                    <ul class="filters__itemwrapper">';
            foreach ( $categories as $category ) {
                echo '<li><a style="color:black" href="' . htmlentities( add_query_arg( 'product_cat', $category->slug, $page_link ) ) . '">' . $category->name . '</a></li>';
            }
            echo '<li><a href="' . htmlentities( remove_query_arg( 'product_cat', $page_link ) ) . '">None</a></li>';
            echo '</ul></div>';
        } ?>
    <div class="productslist__wrapper">
                    <?php $products = new WP_Query( $args );
                        if ( $products->have_posts() ){
                            while ( $products->have_posts() ) {
                                $products->the_post(); ?>
                                <div>
                                    <img src="<?php echo get_field('product_gallery')[0]['url'] ?>" />
                                    <h3><?php echo $products->post->post_title; ?></h3>
                                </div>
                            <?php } 
                        } else { ?>
                            <div>
                                <img src="<?php echo get_template_directory_uri() ?>/compiled/assets/images/no-results.svg" alt="No products found">
                                <h3>No products found</h3>
                            </div>
                        <?php }
                        wp_reset_postdata();
                    ?>
                </div>
    

    This works fine.

    However, what I need to achieve is instead of the URL being:

    /our-products/?pwb-brand=example-brand

    I’d like to use WP’s rewriting functionality to essentially make it:

    /our-products/pwb-brand/example-brand

    While looking for solutions online, and adding a couple of different rewriting functions to my functions.php file I managed to get my local build to navigate to:

    /our-products/pwb-brand/example-brand

    But instead of the filtered products being displayed it was rendering all products. So it looks like the filter doesn’t work when using rewrites? I could be mistaken however.

    Any help would be appreciated.

    • This topic was modified 3 years, 5 months ago by CharlieCoplestone.
    • This topic was modified 3 years, 5 months ago by James Huff.
    • This topic was modified 3 years, 5 months ago by James Huff.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hey @charliecoplestone,

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the following places for more development-oriented questions:

    Cheers

    Thread Starter CharlieCoplestone

    (@charliecoplestone)

    @3sonsdevelopment

    Hi there, I actually solved this in a pretty interesting way. But likely not the most efficient.

    <section class="section--titlealt">
        <div class="container">
            <h1 class="content__title content__title--center">
                <?php echo the_title(); ?>
            </h1>
        </div>
        <?php get_template_part('template-parts/products/products-banner-image'); ?>
        <div class="container">
            <div class="products__wrapper">
                <div class="filters__wrapper">
                    <button class="filters__toggle" data-filter-control="">Filter Products</button>
                    <div class="filters__container" data-filter-target>
                    <div class="filters__closefilter" data-filter-close></div>
                    <?php
                        global $wp;
                        $page_link = get_permalink();
                        $categories = get_terms( 'product_cat' );
                        $brands = get_terms( 'pwb-brand' );
                        $current_url = home_url(add_query_arg(array(), $wp->request));
    
                        if (get_query_var('pwb-brand') !== "" && get_query_var('product_cat') == "") {
                            $args = array(
                                'post_type'         => 'product',
                                'paged'             => ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1,
                                'posts_per_page'    => 200,
                                'post_status'       => "publish",
                                'tax_query'         => array(
                                    array(
                                        'taxonomy'      => 'pwb-brand',
                                        'field'         => 'slug', //This is optional, as it defaults to 'term_id'
                                        'terms'         => get_query_var('pwb-brand') !== "" ? get_query_var('pwb-brand') : "",
                                        'operator'      => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
                                    ),
                                )
                            );
                        } else if (get_query_var('pwb-brand') == "" && get_query_var('product_cat') !== "") {
                            $args = array(
                                'post_type'         => 'product',
                                'paged'             => ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1,
                                'posts_per_page'    => 200,
                                'product_cat'       => get_query_var('product_cat'),
                                'post_status'       => "publish",
                            );
                        } else if (get_query_var('pwb-brand') !== "" && get_query_var('product_cat') !== "") {
                            $args = array(
                                'post_type'         => 'product',
                                'paged'             => ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1,
                                'posts_per_page'    => 200,
                                'product_cat'       => get_query_var('product_cat'),
                                'post_status'       => "publish",
                                'tax_query'         => array(
                                    array(
                                        'taxonomy'      => 'pwb-brand',
                                        'field'         => 'slug', //This is optional, as it defaults to 'term_id'
                                        'terms'         => get_query_var('pwb-brand') !== "" ? get_query_var('pwb-brand') : "",
                                        'operator'      => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
                                    ),
                                )
                            );
                        } else {
                            $args = array(
                                'post_type'         => 'product',
                                'paged'             => ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1,
                                'posts_per_page'    => 200,
                                'post_status'       => "publish",
                            );
                        }
    
                        if ( isset( $_GET['product_cat'] ) ) {
                            $args['product_cat'] = $_GET['product_cat'];
                            $page_link = add_query_arg( 'product_cat', $_GET['product_cat'], $page_link );
                        }
                        if ( isset( $_GET['pwb-brand'] ) ) {
                            $args['pwb-brand'] = $_GET['pwb-brand'];
                            $page_link = add_query_arg( 'pwb-brand', $_GET['pwb-brand'], $page_link );
                        }
    
                        if ( is_array( $brands ) ) {
                            echo '<div class="filters__brand">
                                    <div class="filters__tab" data-filter-toggle="">
                                        <span class="filters__iden">Brands</span>
                                        <span class="filters__close"></span>
                                    </div>
                                    <div class="filters__itemwrapper">';
                            foreach ( $brands as $brand ) {
                                if (get_query_var('pwb-brand') == $brand->slug && get_query_var('product_cat') == "") {
                                    echo '<div class="filters__itemcontainer">
                                            <label class="filters__label">
                                                <input type="checkbox" class="filters__checkbox" checked>
                                                <span class="filters__checkmark"></span>
                                            </label>
                                            <a class="filters__anchor" href="' . get_site_url() . '/our-products">' . $brand->name . '</a>
                                        </div>';
                                } else if (get_query_var('pwb-brand') == $brand->slug && get_query_var('product_cat') !== "") {
                                    echo '<div class="filters__itemcontainer">
                                            <label class="filters__label">
                                                <input type="checkbox" class="filters__checkbox" checked>
                                                <span class="filters__checkmark"></span>
                                            </label>
                                            <a class="filters__anchor" href="' . get_site_url() . "/our-products/product_cat/" . get_query_var('product_cat') . '">' . $brand->name . '</a>
                                        </div>';
                                } else if (get_query_var('product_cat') !== "" && get_query_var('pwb-brand') == $brand->slug) {
                                    echo '<div class="filters__itemcontainer">
                                            <label class="filters__label">
                                                <input type="checkbox" class="filters__checkbox" checked>
                                                <span class="filters__checkmark"></span>
                                            </label>
                                            <a class="filters__anchor" href="' . get_site_url() . "/our-products/product_cat/" . get_query_var('product_cat') . '">' . $brand->name . '</a>
                                        </div>';
                                } else if (get_query_var('product_cat') !== "") {
                                    echo '<div class="filters__itemcontainer">
                                            <label class="filters__label">
                                                <input type="checkbox" class="filters__checkbox">
                                                <span class="filters__checkmark"></span>
                                            </label>
                                            <a class="filters__anchor" href="' . get_site_url() . '/our-products/product_cat/' . get_query_var('product_cat') . htmlentities( add_query_arg( 'pwb-brand', $brand->slug, null ) ) . '">' . $brand->name . '</a>
                                    </div>';
                                } else if (get_query_var('product_cat') == "") {
                                    echo '<div class="filters__itemcontainer">
                                            <label class="filters__label">
                                                <input type="checkbox" class="filters__checkbox">
                                                <span class="filters__checkmark"></span>
                                            </label>
                                            <a class="filters__anchor" href="' . get_site_url() . "/our-products/pwb-brand/" . $brand->slug . '">' . $brand->name . '</a>
                                        </div>';
                                }
                            }
                            echo '<li><a href="' . htmlentities( remove_query_arg( 'product_cat', $page_link ) ) . '">None</a></li>';
                            echo '</div> </div>';
                        }
    
                        if ( is_array( $categories ) ) {
                            echo '<div class="filters__cat">
                                    <div class="filters__tab" data-filter-toggle="">
                                        <span class="filters__iden">Type</span>
                                        <span class="filters__close"></span>
                                    </div>
                                    <div class="filters__itemwrapper">';
                            foreach ( $categories as $category ) {
                                if (get_query_var('product_cat') == $category->slug && get_query_var('pwb-brand') == "") {
                                    echo '<div class="filters__itemcontainer">
                                            <label class="filters__label">
                                                <input type="checkbox" class="filters__checkbox" checked>
                                                <span class="filters__checkmark"></span>
                                            </label>
                                            <a class="filters__anchor" href="' . get_site_url() . '/our-products">' . $category->name . '</a>
                                        </div>';
                                } else if (get_query_var('product_cat') == $category->slug && get_query_var('pwb-brand') !== "") {
                                    echo '<div class="filters__itemcontainer">
                                            <label class="filters__label">
                                                <input type="checkbox" class="filters__checkbox" checked>
                                                <span class="filters__checkmark"></span>
                                            </label>
                                            <a class="filters__anchor" href="' . get_site_url() . "/our-products/pwb-brand/" . get_query_var('pwb-brand') . '">' . $category->name . '</a>
                                        </div>';
                                } else if (get_query_var('pwb-brand') !== "" && get_query_var('product_cat') == $category->slug) {
                                    echo '<div class="filters__itemcontainer">
                                            <label class="filters__label">
                                                <input type="checkbox" class="filters__checkbox">
                                                <span class="filters__checkmark"></span>
                                            </label>
                                            <a class="filters__anchor" href="' . get_site_url() . "/our-products/pwb-brand/" . get_query_var('pwb-brand') . '">' . $category->name . '</a>
                                        </div>';
                                } else if (get_query_var('pwb-brand') !== "") {
                                    echo '<div class="filters__itemcontainer">
                                            <label class="filters__label">
                                                <input type="checkbox" class="filters__checkbox">
                                                <span class="filters__checkmark"></span>
                                            </label>
                                            <a class="filters__anchor" href="' . get_site_url() . '/our-products/pwb-brand/' . get_query_var('pwb-brand') . htmlentities( add_query_arg( 'product_cat', $category->slug, null ) ) . '">' . $category->name . '</a>
                                    </div>';
                                } else if (get_query_var('pwb-brand') == "") {
                                    echo '<div class="filters__itemcontainer">
                                            <label class="filters__label">
                                                <input type="checkbox" class="filters__checkbox">
                                                <span class="filters__checkmark"></span>
                                            </label>
                                            <a class="filters__anchor" href="' . get_site_url() . "/our-products/product_cat/" . $category->slug . '">' . $category->name . '</a>
                                        </div>';
                                }
                            }
                            echo '<li><a href="' . htmlentities( remove_query_arg( 'product_cat', $page_link ) ) . '">None</a></li>';
                            echo '</div> </div>';
                        } ?>
                    </div>
                </div>
    
                <div class="productslist__wrapper" data-products>
                    <?php $products = new WP_Query( $args );
                        if ( $products->have_posts() ){
                            while ( $products->have_posts() ) {
                                $products->the_post();
                                $bullets = get_field('product_list_bullet_points');
                                $brand = get_term_by( 'slug', $products->query['pwb-brand'], 'pwb-brand');
                                $url = get_permalink($products->post->ID);
                                ?>
    
                                <a class="product__container" href="<?php echo $url ?>">
                                    <div class="product__image" style="background-image: url(<?php echo get_field('product_gallery')[0]['sizes']['medium'] ?>)"></div>
                                    <div class="product__infowrapper">
                                        <p class="content__desc content__desc--xsml content__desc--grey content__desc--center">
                                            <?php echo $brand->name ?>
                                        </p>
                                        <p class="content__desc content__desc--bold content__desc--center">
                                            <?php echo $products->post->post_title; ?>
                                        </p>
                                        <ul class="product__list">
                                            <?php
                                            if ($bullets !== false) {
                                                foreach ($bullets as $bullet) { ?>
                                                    <li class="product__listitem"><?php echo $bullet['product_list_bullet_point'] ?></li>
                                                <?php }
                                            } ?>
                                        </ul>
                                        <button class="button product__button">View Product</button>
                                    </div>
                                </a>
                            <?php } 
                        } else { ?>
                            <div class="product__noresWrapper">
                                <img class="product__nores" src="<?php echo get_template_directory_uri() ?>/compiled/assets/images/no-results.svg">
                                <h3 class="content__title content__title--center content__title--nores">No results found</h3>
                                <p class="content__desc content__desc--center">Try a different filter</p>
                            </div>
                        <?php }
                        wp_reset_postdata();
                    ?>
                </div>
            </div> 
        </div>
    </section>

    If that is readable at all. I can essentially filter through the products with both Brands/Categories. Links are rendered based on what’s being filtered. Here’s the sites filter for reference:

    https://marketfresh.co.uk/our-products/

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WordPress Taxonomy URL rewrite whilst keeping filtered posts?’ is closed to new replies.