所以我有一个自定义的帖子类型,并在面包屑中创建了一些链接,以便对帖子进行排序。排序链接设置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 );
答案 0 :(得分:0)
在else部分使用
$args = array ('post_type' =>'inventory', 'post_status' => 'publish','posts_per_page' => -1 );
这样可行