Custom page archive query by url var calling page twice on one of my custom queries?

enter image description here

This is such an odd issue. I will try and break it down as simply as I can.

I am creating my own custom page query which works like an archive using url variables to customise the query results.

This is my getVar() function…

public static function getVar($name, $default = false) {

    // check our param exists
    if(isset($_REQUEST[$name])) {
      return $_REQUEST[$name];
    }

    // return our default value
    return $default;
}

This is my page-products.php entire query…

// get our filter params
$sSearch       = CF::getVar('search', false);
$sCategory     = CF::getVar('cat', false);
$sSweets       = CF::getVar('sweets', false);
$sChocolates   = CF::getVar('chocolate', false);
$sSnacks       = CF::getVar('biscuits-snacks', false);
$sPackaging    = CF::getVar('packaging', false);
$sSorting      = CF::getVar('orderby', 'random');

// build our basic filters
$aFilters = array (
    'post_type'         => 'product',
    'post_status'       => 'publish',
    'posts_per_page'    => 100,
    'orderby'           => 'ASC',
    'tax_query'         => array (
        'relation'          => 'AND',
    )
);

// filter by search query
if($sSearch) {
    $aFilters['s'] = $sSearch;
}

// filter by product category
if($sCategory) {
    // extend our filter array
    $aFilters['tax_query'][] = array (
        'taxonomy'  => 'cat',
        'field'     => 'slug',
        'terms'     => array ($sCategory),
    );
}

// filter by product sweets
if($sSweets) {
    // extend our filter array
    $aFilters['tax_query'][] = array (
        'taxonomy'  => 'confectionery',
        'field'     => 'slug',
        'terms'     => array ($sSweets),
    );
}

// filter by product chocolates
if($sChocolates) {
    // extend our filter array
    $aFilters['tax_query'][] = array (
        'taxonomy'  => 'confectionery',
        'field'     => 'slug',
        'terms'     => array ($sChocolates),
    );
}

// filter by product biscuits & snacks
if($sSnacks) {
    // extend our filter array
    $aFilters['tax_query'][] = array (
        'taxonomy'  => 'confectionery',
        'field'     => 'slug',
        'terms'     => array ($sSnacks),
    );
}

// filter by product packaging
if($sPackaging) {
    // extend our filter array
    $aFilters['tax_query'][] = array (
        'taxonomy'  => 'packaging',
        'field'     => 'slug',
        'terms'     => array ($sPackaging),
    );
}

// sorting select contents array
$aSorting = array (
    'newest'        => 'Latest products',
    'oldest'        => 'Oldest products',
    'product_asc'   => 'Product A-Z',
    'product_desc'  => 'Product Z-A'
);

if(User::logged_in()) {
    // show price sorting in the dropdown
    $aSorting['price_highest']  = 'Price high-low';
    $aSorting['price_lowest']   = 'Price low-high';
}

// determine our sorting filters
switch($sSorting) {

    case 'product_asc':
        $aFilters['orderby']    = 'title';
        $aFilters['order']      = 'ASC';
        break;

    case 'product_desc':
        $aFilters['orderby']    = 'title';
        $aFilters['order']      = 'DESC';
        break;

    case 'price_highest':
        $aFilters['meta_key']   = 'product_starting_price';
        $aFilters['orderby']    = 'meta_value_num';
        $aFilters['order']      = 'DESC';
        break;

    case 'price_lowest':
        $aFilters['meta_key']   = 'product_starting_price';
        $aFilters['orderby']    = 'meta_value_num';
        $aFilters['order']      = 'ASC';
        break;

    case 'oldest':
        $aFilters['orderby']    = 'date';
        $aFilters['order']      = 'ASC';
        break;

    case 'latest':
        $aFilters['orderby']    = 'date';
        $aFilters['order']      = 'DESC';
        break;

    case 'random':
    default:
        $aFilters['orderby']    = 'rand';
        $aFilters['order']      = 'DESC';
        break;
}

// build our product object
$oProducts = new WP_Query($aFilters);

So everything above is working absolutely fine. Each query returns the correct results.

However, on the cat query, I noticed the page was taking a little longer to load than the other queries.

So I checked my network console and this is what is happening…

products?cat=eco-range

As you can see above its loading twice. This doesn’t make any sense?

See below my network results on my other queries which are all one time, as they should be.

products?sweets=millions

products?packaging=gift-box-midi

enter image description here

products?search=110201

enter image description here

If anyone has any ideas as to why this could be happening or can help me solve this issue. That would be great.

Thanks in advance.

Read more here:: Custom page archive query by url var calling page twice on one of my custom queries?

Leave a Reply

Your email address will not be published. Required fields are marked *