2015-09-01 39 views
0

所以我有一個自定義的帖子類型,並在麪包屑中創建了一些鏈接以允許對帖子進行排序。排序鏈接設置URL中的變量,然後拉動這些變量以確定查詢的參數。基於URL變量的Wordpress查詢多個參數集

這是一個混亂的混亂。但它的功能如預期。但是,要做到這一點,必須有比這一堆代碼更好的方式。唯一不使用此代碼的是,如果在URL中設置了NO變量,它將返回NO 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 = ''; 

這裏是混亂的查詢/參數數量的代碼。

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

在其他部分使用

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

這會工作