Advertisement
Guest User

Untitled

a guest
Feb 11th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 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. $all_post_ids = array();
  20. foreach ( $results as $result ) {
  21. if(isset($result->IDs) && $result->IDs){
  22. $all_post_ids = array_merge($all_post_ids ,(array) explode(',', $result->IDs));
  23. }
  24. }
  25.  
  26. $all_post_ids = array_unique($all_post_ids);
  27.  
  28. if(!empty($all_post_ids)){
  29.  
  30. // don't use default pagination
  31. $query->set( 'nopaging', 1 );
  32.  
  33. $query->set( 'posts_per_page', count($all_post_ids) );
  34. $query->set( 'post__in', $all_post_ids );
  35. $query->set( 'orderby', 'post__in' );
  36. $query->set( 'ignore_sticky_posts', 1 );
  37.  
  38. } else {
  39.  
  40. // if no results found -> don't query for posts
  41. add_filter( 'posts_where', 'alter_posts_where' );
  42. }
  43.  
  44. } else {
  45.  
  46. // if no results found -> don't query for posts
  47. add_filter( 'posts_where', 'alter_posts_where' );
  48.  
  49. } // end if($results)
  50. }
  51. }
  52. }
  53.  
  54.  
  55. function alter_posts_where( $where ){
  56. return $where . ' AND 1 = 0';
  57. }
  58.  
  59.  
  60. function get_dates( $dates_per_page, $limit = '', $cat = 1 ) {
  61.  
  62. global $wpdb;
  63.  
  64. $field = '';
  65. $join = '';
  66. $where = "WHERE post_type = 'post' AND post_status = 'publish'";
  67.  
  68. if( '' == $limit ) {
  69. // called from pre_get_post action lets get multiple posts with $cat on top
  70. $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
  71. $field = ", group_concat(DISTINCT ID ORDER BY tt.term_id = '" . $cat . "' DESC, post_date DESC) as IDs";
  72. $join = "INNER JOIN {$wpdb->term_relationships} tr ON ($wpdb->posts.ID = tr.object_id)
  73. INNER JOIN {$wpdb->term_taxonomy} tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)";
  74. $limit = ' LIMIT ' . (($paged -1) * $dates_per_page) . ',' . $dates_per_page;
  75. }
  76.  
  77. $query = "
  78. SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(DISTINCT ID) as posts $field
  79. FROM $wpdb->posts $join $where
  80. GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date)
  81. ORDER BY post_date DESC $limit";
  82.  
  83. return $wpdb->get_results( $query );
  84. }
  85.  
  86.  
  87. function next_date_link( $dates_per_page = 10, $label = 'Next Page' ){
  88. return previous_date_link( $dates_per_page, $label, false );
  89. }
  90.  
  91.  
  92. function previous_date_link( $dates_per_page = 10, $label = 'Previous Page', $prev = true ){
  93.  
  94. $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
  95.  
  96. if ( $prev ) {
  97.  
  98. // limit to one previous post
  99. $count = (($paged -1) * $dates_per_page) + $dates_per_page;
  100. $limit = ' LIMIT ' . $count . ', 1';
  101.  
  102. } else {
  103.  
  104. // limit to one next post
  105. $count = (($paged -1) * $dates_per_page)-1;
  106.  
  107. if ( ($count+1) < $dates_per_page )
  108. return '';
  109.  
  110. $limit = ' LIMIT ' . $count . ', 1';
  111.  
  112. }
  113.  
  114. $results = get_dates( $dates_per_page, $limit );
  115.  
  116. if ( $results ) {
  117. $pagenum = ( $prev ) ? ($paged+1) : ($paged-1);
  118. return '<a href="' . get_pagenum_link( $pagenum ) . '" >' . $label . '</a>';
  119. }
  120.  
  121. return '';
  122. }
  123.  
  124.  
  125. // display pagination function based on twentytwelve_content_nav
  126. // uses the previous_date_link and next_date_link functions
  127. function get_index_pagination_nav( $html_id , $dates_per_page = 10) {
  128.  
  129. $html_id = esc_attr( $html_id );
  130.  
  131. if ( is_home() ) :
  132. // pagination on the home page
  133. $prev = previous_date_link( $dates_per_page, 'Older »' );
  134. $next = next_date_link( $dates_per_page, '« Newer' );
  135. if($prev || $next) : ?>
  136. <nav id="<?php echo $html_id; ?>" class="navigation" role="navigation">
  137. <h3 class="assistive-text"><?php _e( 'Post navigation', 'twentytwelve' ); ?></h3>
  138. <div class="nav-previous alignleft"><?php echo $prev; ?></div>
  139. <div class="nav-next alignright"><?php echo $next; ?></div>
  140. </nav><!-- #<?php echo $html_id; ?> .navigation -->
  141. <?php
  142. endif;
  143.  
  144. else :
  145. // pagination not on the home page
  146. // index.php is used as a fallback template, profide pagination for these pages
  147. // check if the function exists (uses the twentytwelve_content_nav function from Twenty Twelve theme)
  148. if (function_exists( 'twentytwelve_content_nav' ) ) {
  149. twentytwelve_content_nav( $html_id );
  150. }
  151.  
  152. endif;
  153.  
  154. } // end of function get_index_pagination_nav
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement