1

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

enter image description here

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

enter image description here

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.


Unfortunately Chris's answer did not make any change. See the updated changed

My custom category is now registered as prod_cat and my url var is also set to prod_cat so not to conflict with the reserved terms.

https://codex.wordpress.org/Reserved_Terms

https://codex.wordpress.org/WordPress_Query_Vars

Here is my updated query on my page-products.php.

// get our filter params
$sCategory = CF::getVar('prod_cat', false);

// 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 product prod_cat
if($sCategory) {
    // extend our filter array
    $aFilters['tax_query'][] = array (
        'taxonomy'  => 'prod_cat',
        'field'     => 'slug',
        'terms'     => array ($sCategory),
    );
}

And its still loading twice in the network console.

enter image description here


This doesn't make any sense.

  • Where did you see those Network results? Is it when you visit ?prod_cat=chocolate ? – Sally CJ Dec 9 '18 at 0:56
  • 1
    I've literally just figured out what was happening, I'm going to leave an answer shortly but it's probably pointless. I had a typo in my nav walker. So my page is products but I was accidentally calling product. So the extra network loading was because wordpress was redirecting to the correct page. – joshmoto Dec 9 '18 at 0:59
  • Ah, okay. Then WordPress applied canonical redirect, which is why the redirect happened. But it's a good thing to know, so +1 for the comment. – Sally CJ Dec 9 '18 at 1:14
1

I believe this is because cat is a reserved query parameter. Try change it to a different name like 'category' or just 'c'.

If you compare the two URLs before and after the redirect, are they the same or changed slightly?

  • You're right... I thought category was the reserved type all this time. But I guess its just the slug that displays on categories in posts. Thanks codex.wordpress.org/WordPress_Query_Vars – joshmoto Dec 8 '18 at 18:28
  • Unfortunately changing the query vars and even the registered taxonomy did not fix this, see my code above. – joshmoto Dec 8 '18 at 19:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.