Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jun 18th, 2012  |  syntax: None  |  size: 1.93 KB  |  views: 270  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function my_post_queries( $query ) {
  2.   // not an admin page and it is the main query
  3.   if (!is_admin() && $query->is_main_query()){
  4.     if(is_home()){
  5.       $query->set('posts_per_page', 1);
  6.       if ( get_option( 'sticky_posts' ) ) {
  7.          $post_ids = merge_sticky_posts();
  8.          $query->set('ignore_sticky_posts', 1);
  9.          $query->set('post__in', $post_ids);
  10.          $query->set('orderby', 'post__in');
  11.       }
  12.     }
  13.   }
  14. }
  15. add_action( 'pre_get_posts', 'my_post_queries' );
  16.  
  17. function merge_sticky_posts() {
  18.   if ( false === ( $post_ids = get_transient( 'all_post_ids' ) ) ) {
  19.  
  20.     $sticky_posts = get_option( 'sticky_posts' );
  21.  
  22.     $args = array(
  23.        'posts_per_page' => -1,
  24.        'ignore_sticky_posts' => 1,
  25.        'post_status'   => 'publish',
  26.        'no_found_rows' => true,
  27.     );
  28.      
  29.     $posts = get_posts($args);
  30.     if($posts) {
  31.       $post_ids = array();
  32.       foreach ($posts as $post) {
  33.         $post_ids[] = $post->ID;
  34.       }
  35.     }
  36.    
  37.     $post_ids = array_merge($sticky_posts, $post_ids);
  38.     set_transient('all_post_ids', $post_ids);
  39.      
  40.   }
  41.  
  42.   return $post_ids;
  43. }
  44.  
  45.  
  46. // altered version of the sort-query-by-post-in plugin
  47. // http://wordpress.org/extend/plugins/sort-query-by-post-in/
  48. add_filter( 'posts_orderby', 'merged_post_orderby', 10, 2 );
  49. function merged_post_orderby( $sortby, $thequery ) {
  50.  
  51. if ( !empty($thequery->query_vars['post__in']) && isset($thequery->query_vars['orderby']) && $thequery->query_vars['orderby'] == 'post__in' ) {
  52.     $sortby = "find_in_set(ID, '" . implode( ',', $thequery->query_vars['post__in'] ) . "')";
  53. }
  54.    
  55.   return $sortby;
  56. }
  57.  
  58. // delete transient when updating a post
  59. add_action( 'save_post', 'update_all_ids' );
  60. add_action( 'delete_post', 'update_all_ids' );
  61.  
  62. function update_all_ids( $post_id ) {
  63.   //verify post is not a revision
  64.   if ( !wp_is_post_revision( $post_id ) ) {
  65.     delete_transient( 'all_post_ids' );
  66.   }
  67.  
  68. }
clone this paste RAW Paste Data