Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function get_sticky_query_args(){
- // set posts per page here
- $posts_per_page = 6;
- // limit sticky posts (# number or false)
- $limit_sticky_posts = false;
- $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
- $all_stickies = (array) get_option( 'sticky_posts', array() );
- if ( !empty( $all_stickies ) ) {
- /* order sticky IDs by date */
- $args_sticky_ids = array(
- 'include' => $all_stickies,
- 'orderby' => 'date',
- 'order' => 'DESC',
- 'fields' => 'ids',
- 'cache_results' => false
- );
- $all_stickies = get_posts( $args_sticky_ids );
- if ( $limit_sticky_posts && ( (int) $limit_sticky_posts < count( $all_stickies ) ) ) {
- $all_stickies = array_slice( $all_stickies, 0, (int) $limit_sticky_posts );
- }
- }
- $sticky_count = count($all_stickies);
- $sticky_posts = array();
- /* calculate the first page where there are normal posts */
- $mixed_page = ceil( $sticky_count / $posts_per_page);
- $mixed_page = (($sticky_count % $posts_per_page) == 0) ? $mixed_page + 1 : $mixed_page ;
- /* calculate how many posts are to be shown on the first normal posts page */
- $mixed_posts_per_page = ($posts_per_page * $mixed_page) - $sticky_count;
- if ( !empty( $all_stickies ) && $paged <= $mixed_page ) {
- /* sticky posts for the current page */
- $sticky_posts = (array) array_slice( $all_stickies, ($paged-1)*$posts_per_page, $posts_per_page );
- }
- $sticky_count = count($sticky_posts);
- return compact('posts_per_page', 'paged', 'all_stickies', 'sticky_posts', 'sticky_count', 'mixed_page', 'mixed_posts_per_page');
- }
- add_action( 'pre_get_posts', 'minimize_home_query' );
- function minimize_home_query( $query ) {
- /* not an admin page and is it's the main query */
- if ( !is_admin() && $query->is_main_query() ) {
- /* minimize the query for the home page */
- if ( is_home() ) {
- $query->set( 'fields', 'ids' );
- $query->set( 'posts_per_page', 1 );
- $query->set( 'ignore_sticky_posts', 1 );
- $query->set( 'no_found_rows', true );
- $query->set( 'cache_results', 0 );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement