正如toscho所说,您可以在pre_get_posts
钩该钩子获取作为参数传递的查询对象,因此您不必读取全局变量。
add_action( \'pre_get_posts\', \'wpse12692_pre_get_posts\' );
function wpse12692_pre_get_posts( &$wp_query )
{
if( isset( $_SESSION[\'size\'] ) && $_SESSION[\'size\'] != \'all\' )
{
$wp_query->query_vars[\'meta_query\'] = array(
\'key\' => \'cc_size\',
\'value\' => $_SESSION[\'size\'],
);
}
if( isset( $_SESSION[\'gender\'] ) && $_SESSION[\'gender\'] != \'all\' )
{
$wp_query->query_vars[\'meta_query\'] = array(
\'key\' => \'cc_gender\',
\'value\' => $_SESSION[\'gender\'],
);
}
$wp_query->query_vars[\'orderby\'] = \'post-title\';
// The next line is redundant, get_query_vars reads it from the global $wp_query object
$wp_query->query_vars[\'paged\'] = get_query_var(\'paged\');
}
我发现您的查询依赖于会话变量。这会使将指向页面的链接转发给其他人变得更加困难。你有没有考虑过把这个放在URL中并从那里读取它?您可以通过创建额外的重写规则来做到这一点。