Thoughts on Design, Code, Technology, and Architecture.
67a2 is the work of Cambridge, MA designer Paul Voulgaris.
Contact Us  Contact Us

Main Site: 67a2.com  Portfolio

Facebook: facebook.com/67a2media  Facebook

Twitter: twitter.com/67a2  Twitter

Subscribe to our RSS feed  RSS
 


tag: code  



WordPress: order 5 categories by the most recent posts  

Friday, March 18th, 2011    

A client site has 5 main categories that make up it’s mission. All posts are organized by these 5.
We want the category of the most recent post to be at the top of the list, followed by 3 posts from it.

Here’s my effort at this simple enough sounding problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<!--?php ///////////////   CREATE A CATEGORY ARRAY ///////////////
$cat_array = array();
$args=array(
  'post_type' =--> 'post',
  'post_status' => 'publish',
  'posts_per_page' =>10 //number of posts to collect from... could be higher, I chose 10
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  while ($my_query->have_posts()) : $my_query->the_post();
    $cat_args=array('orderby' => 'none');
    $cats = wp_get_post_terms( $post->ID , 'category', $cat_args);
    foreach($cats as $cat) {
      $cat_array[$cat->term_id] = $cat->term_id;
    }
  endwhile;
}
// remove categories that are not the main 5
//I want these 5 specific cats to show - their category ID's are 31,32,33,34,35
if ($cat_array) {
  foreach($cat_array as $cat)  {
    $category = get_term_by('ID',$cat, 'category');
    if ( $category->term_id == 31 ||
         $category->term_id == 32 ||
         $category->term_id == 33 ||
         $category->term_id == 34 ||
         $category->term_id == 35 ){
    }else{
      unset($cat_array[$category->term_id]);
    }
  }
}
//make sure all of the 5 main categories are in the 10 test posts
//otherwise I'll add them to the end of the array (hack)
if (!in_array(31, $cat_array)) {  $cat_array[31] = 31; }
if (!in_array(32, $cat_array)) {  $cat_array[32] = 32; }
if (!in_array(33, $cat_array)) {  $cat_array[33] = 33; }
if (!in_array(34, $cat_array)) {  $cat_array[34] = 34; }
if (!in_array(35, $cat_array)) {  $cat_array[35] = 35; }
 
///////////////   GET POSTS FOR EACH CATEGORY  ///////////////
// GET THE CATEGORY
if ($cat_array) {
  foreach($cat_array as $cat)  {
    $category = get_term_by('ID',$cat, 'category');
    $args=array(
      'cat' => $category->term_id,
      'showposts'=>3, //number of posts to show
      'caller_get_posts'=>1
    );
    $post_count = 1; //reset post count to 1
    // GET THE POSTS
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post();
 
	 //////////////////////////////////////// YOUR CATEGORY HERE
	if( $post_count == 1){
   	  echo '' . $category->name .'';
	}
        //////////////////////////////////////// YOUR POSTS HERE
        echo the_title() .'';
 
      $post_count++; endwhile;
    }
    wp_reset_query();
  }
}
wp_reset_query(); ?>
 
tags: ,    comments: 0
  • Facebook
  • Twitter
  • LinkedIn
  • Google Bookmarks
  • Tumblr
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Add to favorites
  • email