SHARE
TWEET

Mengetahui Jumlah Post Type Tertentu di Suatu Kategori

pustakakoran Apr 30th, 2017 (edited) 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Misal post dengan type video di kategori work
  2.  
  3. Cara 1:
  4.  
  5. <?php $posts = get_posts('post_type=videos&category=4');
  6. $count = count($posts);
  7. echo $count;
  8. ?>
  9.  
  10. Cara 2 (pakai  WP_Query >> http://codex.wordpress.org/Class_Reference/WP_Query)
  11. <?php
  12. $args = array(
  13.   'cat' => 4,
  14.   'post_type' => 'videos'
  15. );
  16. $the_query = new WP_Query( $args );
  17. echo $the_query->found_posts;
  18. ?>
  19.  
  20. Cara 3: (doc>> https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters)
  21. <?php
  22. $the_query = new WP_Query( array(
  23.     'post_type' => 'CUSTOM_POST_TYPE',
  24.     'tax_query' => array(
  25.         array(
  26.             'taxonomy' => 'CUSTOM_TAXONOMY',
  27.             'field' => 'id',
  28.             'terms' => TERM_ID
  29.         )
  30.     )
  31. ) );
  32. $count = $the_query->found_posts;
  33. ?>
  34.  
  35. ===================
  36. CARA LAIN
  37. ===================
  38. KETEMU DI >> https://wordpress.org/support/topic/display-number-of-posts-per-category/
  39. Pasang di function:
  40. function number_postpercat($idcat) {
  41.     global $wpdb;
  42.     $query = "SELECT count FROM $wpdb->term_taxonomy WHERE term_id = $idcat";
  43.     $num = $wpdb->get_col($query);
  44.     echo $num[0];
  45. }
  46.  
  47. Pasang di theme:
  48. <?php number_postpercat (4); ?>
  49.  
  50.  
  51. =======================================
  52. KODE LIST DAFTAR CATEGORI dan COUNT POS
  53. =======================================
  54. Pasang di function:
  55. function number_postpercat($idcat) {
  56.     global $wpdb;
  57.     $query = "SELECT count FROM $wpdb->term_taxonomy WHERE term_id = $idcat";
  58.     $num = $wpdb->get_col($query);
  59.     echo $num[0];
  60. }
  61.  
  62. Pasang di theme:
  63. foreach (get_categories(array('hide_empty'=>false)) as $category)
  64. {
  65.     echo '<li><a href="' . get_bloginfo('wpurl') .
  66.     '/category/' . $category->category_nicename . '/">' .
  67.     $category->cat_name . '</a> // ' . $category->count .
  68.     'posts</li>';
  69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top