function my_post_queries( $query ) {
// not an admin page and it is the main query
if (!is_admin() && $query->is_main_query()){
if(is_home()){
$query->set('posts_per_page', 1);
if ( get_option( 'sticky_posts' ) ) {
$post_ids = merge_sticky_posts();
$query->set('ignore_sticky_posts', 1);
$query->set('post__in', $post_ids);
$query->set('orderby', 'post__in');
}
}
}
}
add_action( 'pre_get_posts', 'my_post_queries' );
function merge_sticky_posts() {
if ( false === ( $post_ids = get_transient( 'all_post_ids' ) ) ) {
$sticky_posts = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => -1,
'ignore_sticky_posts' => 1,
'post_status' => 'publish',
'no_found_rows' => true,
);
$posts = get_posts($args);
if($posts) {
$post_ids = array();
foreach ($posts as $post) {
$post_ids[] = $post->ID;
}
}
$post_ids = array_merge($sticky_posts, $post_ids);
set_transient('all_post_ids', $post_ids);
}
return $post_ids;
}
// altered version of the sort-query-by-post-in plugin
// http://wordpress.org/extend/plugins/sort-query-by-post-in/
add_filter( 'posts_orderby', 'merged_post_orderby', 10, 2 );
function merged_post_orderby( $sortby, $thequery ) {
if ( !empty($thequery->query_vars['post__in']) && isset($thequery->query_vars['orderby']) && $thequery->query_vars['orderby'] == 'post__in' ) {
$sortby = "find_in_set(ID, '" . implode( ',', $thequery->query_vars['post__in'] ) . "')";
}
return $sortby;
}
// delete transient when updating a post
add_action( 'save_post', 'update_all_ids' );
add_action( 'delete_post', 'update_all_ids' );
function update_all_ids( $post_id ) {
//verify post is not a revision
if ( !wp_is_post_revision( $post_id ) ) {
delete_transient( 'all_post_ids' );
}
}