Advertisement
Guest User

Untitled

a guest
Feb 11th, 2013
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. // change the query before posts are retrieved from the database
  2. add_action( 'pre_get_posts', 'my_post_queries1' );
  3.  
  4. function my_post_queries1( $query ) {
  5.  
  6. // not an admin page and is the main query
  7. if ( !is_admin() && $query->is_main_query() ) {
  8.  
  9. // only alter the query on the home page
  10. if ( is_home() ) {
  11.  
  12. // change these two values
  13. $dates_per_page = 4; // four dates to loop through (dates can have multiple posts)
  14. $top_category_id = 1; // category you want on top of same date posts
  15.  
  16. $results = get_dates( $dates_per_page, '', $top_category_id);
  17.  
  18. if ( $results ) {
  19.  
  20. $all_post_ids = array();
  21. foreach ( $results as $result ) {
  22.  
  23. if( isset( $result->IDs ) && $result->IDs ){
  24.  
  25. $first = array();
  26. $last = array();
  27.  
  28. $post_ids = explode( ',', (string) $result->IDs );
  29.  
  30. if( !empty( $post_ids ) ){
  31.  
  32. foreach((array) $post_ids as $post_id) {
  33. echo $post_id;
  34. if ( is_object_in_term( $post_id, 'category', $top_category_id ) ) {
  35. $first[] = $post_id;
  36. } else {
  37. $last[] = $post_id;
  38. }
  39.  
  40. }
  41.  
  42. $all_post_ids = array_merge($all_post_ids , $first);
  43. $all_post_ids = array_merge($all_post_ids , $last);
  44. $all_post_ids = array_unique($all_post_ids);
  45.  
  46. }
  47. }
  48. }
  49.  
  50. if(!empty($all_post_ids)){
  51.  
  52. // don't use default pagination
  53. $query->set( 'nopaging', 1 );
  54.  
  55. $query->set( 'posts_per_page', count($all_post_ids) );
  56. $query->set( 'post__in', $all_post_ids );
  57. $query->set( 'orderby', 'post__in' );
  58. $query->set( 'ignore_sticky_posts', 1 );
  59.  
  60. } else {
  61.  
  62. // if no results found -> don't query for posts
  63. add_filter( 'posts_where', 'alter_posts_where' );
  64. }
  65.  
  66. } else {
  67.  
  68. // if no results found -> don't query for posts
  69. add_filter( 'posts_where', 'alter_posts_where' );
  70.  
  71. } // end if($results)
  72. } //is home
  73. } // main query
  74. }
  75.  
  76.  
  77. function alter_posts_where( $where ){
  78. return $where . ' AND 1 = 0';
  79. }
  80.  
  81.  
  82. function get_dates( $dates_per_page, $limit = '', $cat = 1 ) {
  83.  
  84. global $wpdb;
  85.  
  86. $field = '';
  87. $where = "WHERE post_type = 'post' AND post_status = 'publish'";
  88.  
  89. if( '' == $limit ) {
  90. // called from pre_get_post action lets get multiple posts with $cat on top
  91. $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
  92. $field = ", group_concat(DISTINCT ID ORDER BY post_date DESC) as IDs";
  93. $limit = ' LIMIT ' . (($paged -1) * $dates_per_page) . ',' . $dates_per_page;
  94. }
  95.  
  96. $query = "
  97. SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth` $field
  98. FROM $wpdb->posts $where
  99. GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date)
  100. ORDER BY post_date DESC $limit";
  101.  
  102. return $wpdb->get_results( $query );
  103. }
  104.  
  105.  
  106. function next_date_link( $dates_per_page = 10, $label = 'Next Page' ){
  107. return previous_date_link( $dates_per_page, $label, false );
  108. }
  109.  
  110.  
  111. function previous_date_link( $dates_per_page = 10, $label = 'Previous Page', $prev = true ){
  112.  
  113. $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
  114.  
  115. if ( $prev ) {
  116.  
  117. // limit to one previous post
  118. $count = (($paged -1) * $dates_per_page) + $dates_per_page;
  119. $limit = ' LIMIT ' . $count . ', 1';
  120.  
  121. } else {
  122.  
  123. // limit to one next post
  124. $count = (($paged -1) * $dates_per_page)-1;
  125.  
  126. if ( ($count+1) < $dates_per_page )
  127. return '';
  128.  
  129. $limit = ' LIMIT ' . $count . ', 1';
  130.  
  131. }
  132.  
  133. $results = get_dates( $dates_per_page, $limit );
  134.  
  135. if ( $results ) {
  136. $pagenum = ( $prev ) ? ($paged+1) : ($paged-1);
  137. return '<a href="' . get_pagenum_link( $pagenum ) . '" >' . $label . '</a>';
  138. }
  139.  
  140. return '';
  141. }
  142.  
  143.  
  144. // display pagination function based on twentytwelve_content_nav
  145. // uses the previous_date_link and next_date_link functions
  146. function get_index_pagination_nav( $html_id , $dates_per_page = 10) {
  147.  
  148. $html_id = esc_attr( $html_id );
  149.  
  150. if ( is_home() ) :
  151. // pagination on the home page
  152. $prev = previous_date_link( $dates_per_page, 'Older »' );
  153. $next = next_date_link( $dates_per_page, '« Newer' );
  154. if($prev || $next) : ?>
  155. <nav id="<?php echo $html_id; ?>" class="navigation" role="navigation">
  156. <h3 class="assistive-text"><?php _e( 'Post navigation', 'twentytwelve' ); ?></h3>
  157. <div class="nav-previous alignleft"><?php echo $prev; ?></div>
  158. <div class="nav-next alignright"><?php echo $next; ?></div>
  159. </nav><!-- #<?php echo $html_id; ?> .navigation -->
  160. <?php
  161. endif;
  162.  
  163. else :
  164. // pagination not on the home page
  165. // index.php is used as a fallback template, profide pagination for these pages
  166. // check if the function exists (uses the twentytwelve_content_nav function from Twenty Twelve theme)
  167. if (function_exists( 'twentytwelve_content_nav' ) ) {
  168. twentytwelve_content_nav( $html_id );
  169. }
  170.  
  171. endif;
  172.  
  173. } // end of function get_index_pagination_nav
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement