Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1.  
  2. function get_sticky_query_args(){
  3.  
  4. // set posts per page here
  5. $posts_per_page = 6;
  6.  
  7. // limit sticky posts (# number or false)
  8. $limit_sticky_posts = false;
  9.  
  10. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  11.  
  12. $all_stickies = (array) get_option( 'sticky_posts', array() );
  13.  
  14. if ( !empty( $all_stickies ) ) {
  15.  
  16. /* order sticky IDs by date */
  17. $args_sticky_ids = array(
  18. 'include' => $all_stickies,
  19. 'orderby' => 'date',
  20. 'order' => 'DESC',
  21. 'fields' => 'ids',
  22. 'cache_results' => false
  23. );
  24.  
  25. $all_stickies = get_posts( $args_sticky_ids );
  26.  
  27. if ( $limit_sticky_posts && ( (int) $limit_sticky_posts < count( $all_stickies ) ) ) {
  28. $all_stickies = array_slice( $all_stickies, 0, (int) $limit_sticky_posts );
  29. }
  30.  
  31. }
  32.  
  33. $sticky_count = count($all_stickies);
  34. $sticky_posts = array();
  35.  
  36. /* calculate the first page where there are normal posts */
  37. $mixed_page = ceil( $sticky_count / $posts_per_page);
  38. $mixed_page = (($sticky_count % $posts_per_page) == 0) ? $mixed_page + 1 : $mixed_page ;
  39.  
  40. /* calculate how many posts are to be shown on the first normal posts page */
  41. $mixed_posts_per_page = ($posts_per_page * $mixed_page) - $sticky_count;
  42.  
  43. if ( !empty( $all_stickies ) && $paged <= $mixed_page ) {
  44.  
  45. /* sticky posts for the current page */
  46. $sticky_posts = (array) array_slice( $all_stickies, ($paged-1)*$posts_per_page, $posts_per_page );
  47.  
  48. }
  49.  
  50. $sticky_count = count($sticky_posts);
  51.  
  52. return compact('posts_per_page', 'paged', 'all_stickies', 'sticky_posts', 'sticky_count', 'mixed_page', 'mixed_posts_per_page');
  53.  
  54. }
  55.  
  56.  
  57. add_action( 'pre_get_posts', 'minimize_home_query' );
  58. function minimize_home_query( $query ) {
  59.  
  60. /* not an admin page and is it's the main query */
  61. if ( !is_admin() && $query->is_main_query() ) {
  62.  
  63. /* minimize the query for the home page */
  64. if ( is_home() ) {
  65.  
  66. $query->set( 'fields', 'ids' );
  67. $query->set( 'posts_per_page', 1 );
  68. $query->set( 'ignore_sticky_posts', 1 );
  69. $query->set( 'no_found_rows', true );
  70. $query->set( 'cache_results', 0 );
  71.  
  72. }
  73.  
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement