修改公告的现有查询和变更单

时间:2016-10-26 作者:kiarashi

这是我的查询设置

        $recipe_args = array(
            \'post_type\'         => \'recipes\',
            \'post_status\'       => \'publish\',               
            \'posts_per_page\'    => $per_page,               
            \'author\'            => $author->ID, 
            \'paged\'             => $paged,
        );

        $recipes = new WP_Query( $recipe_args );
使用此下拉框,我想更改查询结果

        <select name="orderby" class="orderby">
                <option value="menu_order">Default sorting</option>
                <option value="popularity">Sort by popularity</option>
                <option value="rating">Sort by average rating</option>
                <option value="date">Sort by newness</option>
                <option value="price">Sort by price: low to high</option>
                <option value="price-desc">Sort by price: high to low</option>
        </select>
url结构为/作者姓名/?页面=配方(使用add_query_arg() 功能)

我接下来应该做什么,以便在选择下拉选项时加载新页面,例如/作者姓名/?页码=配方(&P);orderby=将以下代码添加到主WP\\U查询的流行程度。

        $recipe_args[\'order\'] = \'DESC\';
        $recipe_args[\'orderby\'] = \'meta_value_num\';         
        $recipe_args[\'meta_query\'][] = array(
                    \'key\'     => \'km_receipes_sales\',
                    \'compare\' => \'>=\',
                    \'value\' => \'0.01\',
            );
非常感谢您的帮助。

1 个回复
SO网友:kiarashi

可以通过首先注册自定义查询变量来解决此问题。https://codex.wordpress.org/Plugin_API/Filter_Reference/query_vars

然后接下来我用switch 功能:

global $wp_query;

switch ( $wp_query->query_vars[\'filter\'] ) {
        case \'random\':
            $recipe_args[\'orderby\']  = \'rand\';                  
        break;
}