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

admin2025-06-05  3

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

products?search=110201



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.

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.


This doesn't make any sense.

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

products?search=110201



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/Reserved_Terms

https://codex.wordpress/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.


This doesn't make any sense.

Share Improve this question edited Dec 8, 2018 at 19:44 joshmoto asked Dec 8, 2018 at 17:09 joshmotojoshmoto 4676 silver badges19 bronze badges 3
  • Where did you see those Network results? Is it when you visit ?prod_cat=chocolate ? – Sally CJ Commented Dec 9, 2018 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 Commented Dec 9, 2018 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 Commented Dec 9, 2018 at 1:14
Add a comment  | 

1 Answer 1

Reset to default 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?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749115226a316491.html

最新回复(0)