2016-10-08 194 views
2

這是我有:

$args = array(
     'type' => 'post', 
     'child_of' => 0, 
     'parent' => '', 
     'orderby' => 'name', 
     'order' => 'ASC', 
     'hide_empty' => 1, 
     'hierarchical' => 1, 
     'exclude' => '', 
     'include' => '', 
     'number' => '', 
     'taxonomy' => 'directory-category', 
     'pad_counts' => false 
    ); 

這讓我的類別。顯示子類別 - WordPress的

我想要的是獲取此directory-category分類的子類別。

關於如何做到這一點的任何想法?

我不是要求解決方案,只是建議或某人向我展示道路。

谷歌搜索並沒有幫助:/

下面是截圖HERE

回答

1

你說你不想直接回答,但本質上你想使用這裏找到get_terms:

https://developer.wordpress.org/reference/functions/get_terms/

SELECT * from prod_term_taxonomy WHERE parent = 0;

UPDATE:

// Using your specific parent taxonomy id of 214 the query is below 
global $wpdb; 
$results = $wpdb->get_results("SELECT * from prod_term_taxonomy WHERE taxonomy = 'directory-category'"); 

// then you can use WordPress get_term to query each object to get it's name based on it's term_id. $results will be an array of objects so you will use a foreach loop to loop through to get each $result like this... 

$child_cat_array = array(); 

foreach ($results as $result) { 
    $term = get_term($result->term_id, $taxonomy); 
    $name = $term->name; 
    // the slug will be used for querying for posts 
    $slug = $term->slug; 
    // this will push the slug of the child category into the array for querying posts later 
    array_push($child_cat_array, $slug); 
} 

然後,您可以修改您的get_posts這樣的查詢:

$args = array(
    'tax_query' => array(
     array(
      'taxonomy' => 'directory-category', 
      'field' => 'slug', 
      'terms' => $child_cat_array 
     ) 
    ) 
); 

$postslist = get_posts($args); 
+0

你希望得到與所有這些子類別相關的帖子嗎? –

+0

感謝您的建議。我已經檢查過'get_terms()',但我正在尋找一些東西,只是更具體一些,lol –

+0

您可能需要用全局$ wpdb查詢WordPress數據庫。如果你願意,我可以給你答案:)你熟悉SQL嗎?因爲你會查詢他們住在你的WordPress數據庫中的確切表格。不幸的是,沒有內置的WordPress功能來檢索它們。 –