通过url变量调用页面对我的一个自定义查询进行两次的自定义页面存档查询?

时间:2018-12-08 作者:joshmoto

这是一个很奇怪的问题。我会尽可能简单地将其分解。

我正在创建自己的自定义页面查询,其工作方式类似于使用url变量自定义查询结果的存档。


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);

所以上面的一切都很好。每个查询都返回正确的结果。

然而,在cat 查询时,我注意到页面的加载时间比其他查询要长一些。

所以我检查了我的网络控制台,这就是正在发生的事情。。。

products?cat=eco-range

enter image description here

正如您在上面看到的,它装载了两次。这没有任何意义?

查看下面我的其他查询的网络结果,这些查询都是一次性的,应该是这样的。

products?sweets=millions

enter image description here

products?packaging=gift-box-midi

enter image description here

products?search=110201

enter image description here



如果有人知道为什么会发生这种情况,或者可以帮助我解决这个问题。那太好了。

提前谢谢。

不幸的是,克里斯的回答没有任何改变。查看更新的已更改

我的自定义类别现在注册为prod_cat 我的url变量也设置为prod_cat 所以不要与保留条款冲突。

https://codex.wordpress.org/Reserved_Terms

https://codex.wordpress.org/WordPress_Query_Vars

这是我在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),
    );
}
它仍然在网络控制台中加载两次。

enter image description here


这没有任何意义。

1 个回复
SO网友:Chris

我认为这是因为cat是一个保留的查询参数。尝试将其更改为其他名称,如“category”或“c”。

如果比较重定向前后的两个URL,它们是相同的还是略有变化?