Wordpress查询基于URL变量设置的多个参数


Wordpress Query Multiple Arguments Set Based on URL Variables

所以我有一个自定义的帖子类型,并在面包屑中创建了一些链接,以便对帖子进行排序。排序链接在URL中设置变量,然后提取这些变量以确定查询的参数。

这是一个混乱的混乱。但它的功能和预期一样。然而,肯定有比这堆乱七八糟的代码更好的方法。这段代码唯一不工作的是,如果URL中没有设置变量,它将返回NO RESULTS而不是ALL RESULTS。

我完全开放,如果有人有一个更好的方法来排序不同的元键/值在飞行/链接帖子。

这些是它从URL中提取的变量:

$sortby = $_GET['sort'] or $sortby = 'price';
$direction = $_GET['dir'] or $direction = 'desc';
$automake = $_GET['make'] or $automake = '';
$autocat = $_GET['model'] or $autocat = '';
$searchkey = $_GET['s'] or $searchkey = '';
$autocondition = $_GET['cond'] or $autocondition = '';

这里是混乱的Query/Args代码。

if ( $s !== '' ) {
$args = array (
'post_type'              => 'inventory',
'post_status'            => 'publish',
'posts_per_page'         => -1,
's'                      => $searchkey,
'order'                  => $direction,
'orderby'                => $orderby,
'meta_key'               => $sorting,
'meta_query'             => array(
    array(
        'key'       => '_auto_sold',
        'value'     => 'No',
        'compare'   => '=',
        'type'      => 'CHAR',
    ),
),
);
} elseif ( $autocondition !== '' ) {
$args = array (
'post_type'              => 'inventory',
'post_status'            => 'publish',
'posts_per_page'         => -1,
's'                      => $searchkey,
'order'                  => $direction,
'orderby'                => $orderby,
'meta_key'               => $sorting,
'meta_query'             => array(
            'relation' => 'AND',
    array(
        'key'       => '_auto_sold',
        'value'     => 'No',
        'compare'   => '=',
        'type'      => 'CHAR',
    ),
    array(
        'key'       => '_auto_status',
        'value'     => $autocondition,
        'compare'   => '=',
        'type'      => 'CHAR',
    ),
),
);
} elseif ( $automake AND $autocat !== '' ) {
$args = array (
'post_type'              => 'inventory',
'post_status'            => 'publish',
'posts_per_page'         => -1,
'order'                  => $direction,
'orderby'                => $orderby,
'meta_key'               => $sorting,
'meta_query'             => array(
            'relation' => 'AND',
    array(
        'key'       => '_auto_sold',
        'value'     => 'No',
        'compare'   => '=',
        'type'      => 'CHAR',
    ),
    array(
        'key'       => '_auto_make',
        'value'     => $automake,
        'compare'   => '=',
        'type'      => 'CHAR',
    ),
    array(
        'key'       => '_auto_model',
        'value'     => $autocat,
        'compare'   => '=',
        'type'      => 'CHAR',
    ),
),
);
} elseif ( $automake !== '' ) {
$args = array (
'post_type'              => 'inventory',
'post_status'            => 'publish',
'posts_per_page'         => -1,
'order'                  => $direction,
'orderby'                => $orderby,
'meta_key'               => $sorting,
'meta_query'             => array(
            'relation' => 'AND',
    array(
        'key'       => '_auto_sold',
        'value'     => 'No',
        'compare'   => '=',
        'type'      => 'CHAR',
    ),
    array(
        'key'       => '_auto_make',
        'value'     => $automake,
        'compare'   => '=',
        'type'      => 'CHAR',
    ),
),
);
} elseif ( $autocat !== '' ) {
$args = array (
'post_type'              => 'inventory',
'post_status'            => 'publish',
'posts_per_page'         => -1,
'order'                  => $direction,
'orderby'                => $orderby,
'meta_key'               => $sorting,
'meta_query'             => array(
            'relation' => 'AND',
    array(
        'key'       => '_auto_sold',
        'value'     => 'No',
        'compare'   => '=',
        'type'      => 'CHAR',
    ),
    array(
        'key'       => '_auto_model',
        'value'     => $autocat,
        'compare'   => '=',
        'type'      => 'CHAR',
    ),
),
);
} else {
$args = array (
'post_type'              => 'inventory',
'post_status'            => 'publish',
'posts_per_page'         => -1,
'order'                  => $direction,
'orderby'                => $orderby,
'meta_key'               => $sorting,
'meta_query'             => array(
    array(
        'key'       => '_auto_sold',
        'value'     => 'No',
        'compare'   => '=',
        'type'      => 'CHAR',
    ),
),
);
}
// The Query
$query = new WP_Query( $args );

在else部分使用

$args = array ('post_type' =>'inventory', 'post_status' => 'publish','posts_per_page' => -1 );