2017-03-29 35 views
0

什麼是最好的和最短的方式在Wordpress到get_categories排序最後發佈的文章?get_categories通過最後發佈的訂單

意味着最近的帖子的類別應該首先出現,它是否可用某種方式?

+1

我會首先檢索所有類別,然後通過類別循環獲取最後一篇文章,並根據發佈日期將其存儲在一個數組中,然後對結果數組進行排序 – fabio

回答

3

試試:

function get_sorted_categories($order_by = 'id', $args = array()){ 
    global $wpdb; 

    $category = get_categories($args); 

    $order = [ 
     'id' => 'post.ID', 
     'date' => 'post.post_date', 
     'modified' => 'post.post_modified', 
    ]; 

    $order_by = $order[ $order_by ]; 

    $q = $wpdb->get_results("SELECT tax.term_id FROM `{$wpdb->prefix}term_taxonomy` tax 
    INNER JOIN `{$wpdb->prefix}term_relationships` rel ON rel.term_taxonomy_id = tax.term_id 
    INNER JOIN `{$wpdb->prefix}posts` post ON rel.object_id = post.ID WHERE tax.taxonomy = 'category' AND post.post_type = 'post' AND post.post_status = 'publish' ORDER BY {$order_by} DESC"); 

    $sort = array_flip(array_unique(wp_list_pluck($q, 'term_id'))); 

    usort($category, function($a, $b) use ($sort, $category) { 
     if(isset($sort[ $a->term_id ], $sort[ $b->term_id ]) && $sort[ $a->term_id ] != $sort[ $b->term_id ]) 
      $res = ($sort[ $a->term_id ] > $sort[ $b->term_id ]) ? 1 : -1; 
     else if(!isset($sort[ $a->term_id ]) && isset($sort[ $b->term_id ])) 
      $res = 1; 
     else if(isset($sort[ $a->term_id ]) && !isset($sort[ $b->term_id ])) 
      $res = -1; 
     else 
      $res = 0; 

     return $res; 
    }); 

    return $category; 
} 

print_r(get_sorted_categories()); 
print_r(get_sorted_categories('date')); 
print_r(get_sorted_categories('modified')); 

獲取按類別順序(後ID |發佈日期|後修改的日期)。沒有任何循環和快速!

+0

你是男人!工作很棒:)謝謝。 – tinyCoder

+0

我修改了代碼,以便我可以將一些'$ args'添加到'get_categories()',但是它打破了排序過程,任何想法如何做到這一點?再次感謝! – tinyCoder

+0

Hi @tinyCoder。代碼更新爲處理異常(隱藏空=> false)。但關於其他'$ args',你是否需要將'$ args'傳遞給'get_categories()'函數呢? – MahdiY

1

我認爲最簡單的方法是遍歷post_date訂購的所有帖子,然後將類別保存到數組中。然後,您可以遍歷類別並顯示它們。他們將是有序的。

事情是這樣的:

<?php 

// Initiate array 
$cats = array(); 

// Query arguments 
$args = array(
    'post_type' => 'post', 
    'posts_per_page' => -1, 
    'orderby' = 'post_date'. 
    'order' => 'DESC' 
); 

// The query 
$query = new WP_Query($args); 

// The loop 
if($query->have_posts()) { 
    while($query->have_posts()) { 
     $query->the_post(); 

     // Get the term object 
     $term = get_the_category(); 

     // Make sure the term doesn't already exist in the array 
     if(!array_key_exists($term[0]->ID, $cats)) { 
      // Add the terms to the array 
      $cats[$term[0]->ID] = $term; 
     } 
    } 
} 

foreach($cats as $cid => $cat) { 
    // Loop through the categories here 
} 
?> 

當然,作爲在評論中提及上面的,你也可以做到這一點的其他方式和遍歷類第一則數組排序。這樣的事情可以做:

<?php 

// Initiate array 
$cats = get_categories(); 
$recent_cats = array(); 

foreach($cats as $k => $cat) { 
    // Query arguments 
    $args = array(
     'post_type' => 'post', 
     'posts_per_page' => 1, 
     'orderby' = 'post_date'. 
     'order' => 'DESC', 
     'cat' => $cat->term_id 
    ); 

    // The query 
    $query = new WP_Query($args); 

    // The loop 
    if($query->have_posts()) { 
     while($query->have_posts()) { 
      $query->the_post(); 
      $date_str = strtotime(the_date()); 
      if(!array_key_exists($date_str, $recent_cats)) { 
       $recent_cats[$date_str] = $cat->name; 
      } 
     } 
    } 
} 

krsort($recent_cats); 

// Loop through $recent_cats here 

?> 
+0

感謝您嘗試幫助!我的網站有成千上萬的帖子和幾十個類別,肯定會影響功能,請注意,你有一個拼寫錯誤'orderby'='post_date'.'應該是''orderby'=>'post_date','和代碼返回'array_key_exists():第一個參數應該是一個字符串或一個整數'。 – tinyCoder