Advertisement
Guest User

Untitled

a guest
Feb 9th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. add_action( 'pre_get_posts', 'my_post_queries1' );
  2.  
  3. function my_post_queries1( $query ) {
  4.  
  5. // not an admin page and is the main query
  6. if ( !is_admin() && $query->is_main_query() ) {
  7.  
  8. // only alter the query on the home page
  9. if ( is_home() ) {
  10.  
  11. // change these two values
  12. $dates_per_page = 4; // four dates to loop through (dates can have multiple posts)
  13. $top_category_id = 1; // category you want on top of same date posts
  14.  
  15. $all_posts = array();
  16. $results = get_dates( $dates_per_page );
  17.  
  18. if ( $results ) {
  19.  
  20. $date_sorted = array();
  21. $i = 0;
  22.  
  23. // loop through dates
  24. foreach ( $results as $result ) {
  25.  
  26. $args = array(
  27. 'posts_per_page' => -1,
  28. 'year' => $result->year,
  29. 'monthnum' => $result->month,
  30. 'day' => $result->dayofmonth,
  31. 'fields' => 'ids'
  32. );
  33.  
  34. $date_posts = get_posts($args);
  35.  
  36. if ( $date_posts ) {
  37.  
  38. // sort date posts by category
  39. foreach ( $date_posts as $post_id ) {
  40.  
  41. if ( is_object_in_term( $post_id, 'category', $top_category_id ) ) {
  42. $date_sorted[$i]['first'][] = $post_id;
  43. } else {
  44. $date_sorted[$i]['last'][] = $post_id;
  45. }
  46. }
  47. }
  48. ++$i;
  49. } // endforeach ( $results as $result )
  50.  
  51.  
  52. // all post IDs with category test on top
  53. foreach ( $date_sorted as $key => $date ) {
  54.  
  55. if ( isset( $date['first'] )) {
  56. $all_posts = array_merge($all_posts, $date['first'] );
  57. }
  58. if ( isset( $date['last'] )) {
  59. $all_posts = array_merge($all_posts, $date['last'] );
  60. }
  61. }
  62.  
  63. // don't use default pagination
  64. $query->set( 'nopaging', 1 );
  65. $query->set( 'posts_per_page', count($all_posts) );
  66. $query->set( 'post__in', $all_posts );
  67. $query->set( 'orderby', 'post__in' );
  68. $query->set( 'ignore_sticky_posts', 1 );
  69.  
  70. } else {
  71.  
  72. // if no results found don't query for posts
  73. add_filter( 'posts_where', 'alter_posts_where' );
  74.  
  75. } // end if($results)
  76. }
  77. }
  78. }
  79.  
  80.  
  81. function alter_posts_where( $where ){
  82. return $where . ' AND 1 = 0';
  83. }
  84.  
  85.  
  86. function get_dates( $dates_per_page, $limit = '' ) {
  87.  
  88. $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
  89.  
  90. if( '' == $limit ) {
  91. $limit = ' LIMIT ' . (($paged -1) * $dates_per_page) . ',' . $dates_per_page;
  92. }
  93.  
  94. global $wpdb;
  95. $where = "WHERE post_type = 'post' AND post_status = 'publish'";
  96. $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(ID) as posts FROM $wpdb->posts $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date DESC $limit";
  97.  
  98. return $wpdb->get_results( $query );
  99. }
  100.  
  101.  
  102. function next_date_link( $dates_per_page = 10, $label = 'Next Page' ){
  103. return previous_date_link( $dates_per_page, $label, false );
  104. }
  105.  
  106.  
  107. function previous_date_link( $dates_per_page = 10, $label = 'Previous Page', $prev = true ){
  108.  
  109. $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
  110.  
  111. if ( $prev ) {
  112.  
  113. $count = (($paged -1) * $dates_per_page) + $dates_per_page;
  114. $limit = ' LIMIT ' . $count . ', 1';
  115.  
  116. } else {
  117.  
  118. $count = (($paged -1) * $dates_per_page)-1;
  119.  
  120. if ( ($count+1) < $dates_per_page )
  121. return '';
  122.  
  123. $limit = ' LIMIT ' . $count . ', 1';
  124.  
  125. }
  126.  
  127. $results = get_dates( $dates_per_page, $limit );
  128.  
  129. if ( $results ) {
  130. $pagenum = ( $prev ) ? ($paged+1) : ($paged-1);
  131. return '<a href="' . get_pagenum_link( $pagenum ) . '" >' . $label . '</a>';
  132. }
  133.  
  134. return '';
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement