Snippets

Willy Bahuaud Améliorer la fonctionnalité des « contenus à la une » de WordPress / Improve WordPress “Sticky Posts” feature

Updated by Willy Bahuaud

File sticky-pagination.php Modified

  • Ignore whitespace
  • Hide word diff
 function sticky_count_on_paged( $q ) {
     if ( is_paged_home_with_sticky( $q ) ) {
         $q->set('post__not_in', get_option( 'sticky_posts', array() ) );
+        
         $offset = count( get_option( 'sticky_posts', array() ) );
-        $page_offset = $q->get( 'posts_per_page', get_option( 'posts_per_page' ) ) * ( $q->get( 'paged' ) -1 ) - $offset;
+        $page_offset = $q->get( 'posts_per_page', get_option( 'posts_per_page' ) ) 
+          * ( $q->get( 'paged' ) -1 ) - $offset;
         $q->set( 'offset', $page_offset );
     }
     return $q;
Updated by Willy Bahuaud

File sticky-cpt.php Modified

  • Ignore whitespace
  • Hide word diff
     $post_type = $post->post_type;
     $post_type_object = get_post_type_object( $post_type );
     $can_publish = current_user_can( $post_type_object->cap->publish_posts );
-    return $can_publish && in_array( $post->post_type, array( 'page', 'event' ) );
+    return $can_publish 
+      // Choose your post types
+      && in_array( $post->post_type, array( 'page', 'event' ) );
 }
 
 // Start deferring php output
Updated by Willy Bahuaud

File sticky-cpt.php Modified

  • Ignore whitespace
  • Hide word diff
 function w_post_submitbox_misc_actions( $post ) {
     if ( need_sticky( $post ) ) {
         $temp = ob_get_clean();
-        
+
+        // WP core need two different checkbox: on for authors, and another one for editors
         $sticky_box = '<input type="checkbox" style="display:none" 
             name="hidden_post_sticky" id="hidden-post-sticky" value="sticky" '
             . checked( is_sticky( $post->ID ), true, false ) . ' />';
Updated by Willy Bahuaud

File sticky-cpt.php Modified

  • Ignore whitespace
  • Hide word diff
 <?php
 
+// Does the current user can define sticky posts for this custom post type?
 function need_sticky( $post ) {
     $post_type = $post->post_type;
     $post_type_object = get_post_type_object( $post_type );
     return $can_publish && in_array( $post->post_type, array( 'page', 'event' ) );
 }
 
+// Start deferring php output
 add_action( 'post_submitbox_minor_actions', 'w_post_submitbox_minor_actions' );
 function w_post_submitbox_minor_actions( $post ) {
     if ( need_sticky( $post ) ) {
     }
 }
 
+// Get PHP output and print sticky posts metabox
 add_action( 'post_submitbox_misc_actions', 'w_post_submitbox_misc_actions' );
 function w_post_submitbox_misc_actions( $post ) {
     if ( need_sticky( $post ) ) {

File sticky-pagination.php Modified

  • Ignore whitespace
  • Hide word diff
 <?php
 
+// Add a custom query var to force post count
 add_filter( 'query_vars', 'sticky_offset_query_var' );
 function sticky_offset_query_var( $vars ) {
     $vars[] = 'ppp_include_sticky';
     return $vars;
 }
 
+// Add the query var on home page query
 add_filter( 'pre_get_posts', 'w_sticky_posts' );
 function w_sticky_posts($q) {
     if ( is_home() ) {
     return $q;
 }
 
+// Truncate posts array to adjust post count
 add_filter( 'the_posts', 'sticky_count_offset', 10, 2 );
 function sticky_count_offset( $posts, $q ) {
     if ( $q->get( 'ppp_include_sticky' )
     return $posts;
 }
 
+// Are we on a paged home with sticky posts ?
 function is_paged_home_with_sticky( $q ) {
     return $q->is_home()
       && $q->get( 'ppp_include_sticky' )
       && false == $q->get( 'ignore_sticky_posts' );
 }
 
+// Define page offset to show posts truncated on previous page
 add_filter( 'pre_get_posts', 'sticky_count_on_paged', 20 );
 function sticky_count_on_paged( $q ) {
     if ( is_paged_home_with_sticky( $q ) ) {
     return $q;
 }
 
+// Adjust found_posts property to display the right number of pages
 add_filter( 'found_posts', 'sticky_adjust_offset_pagination', 1, 2 );
 function sticky_adjust_offset_pagination( $found_posts, $q ) {
     if ( is_paged_home_with_sticky( $q ) ) {

File sync-sticky-values-with-acf.php Modified

  • Ignore whitespace
  • Hide word diff
 <?php
 
-// If ACF field `sticky_posts` is on an option page
+/**
+ * If ACF field `sticky_posts` is on an option page
+ */
 add_filter( 'pre_option_options_sticky_posts', 'w_get_sticky' );
 function w_get_sticky() {
     return get_option( 'sticky_posts' );
     }
 }
 
-// If ACF field `sticky_posts` is a page_for_posts metabox
+/**
+ * If ACF field `sticky_posts` is a page_for_posts metabox
+ */
 add_filter( 'get_post_metadata', 'w_get_sticky_post_meta', 10, 3 );
 function w_get_sticky_post_meta( $check, $object_id, $meta_key ) {
     if ( get_option( 'page_for_posts' ) == $object_id
Updated by Willy Bahuaud

File sticky-pagination.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+
+add_filter( 'query_vars', 'sticky_offset_query_var' );
+function sticky_offset_query_var( $vars ) {
+    $vars[] = 'ppp_include_sticky';
+    return $vars;
+}
+
+add_filter( 'pre_get_posts', 'w_sticky_posts' );
+function w_sticky_posts($q) {
+    if ( is_home() ) {
+        $q->set('ppp_include_sticky',true);
+    }
+    return $q;
+}
+
+add_filter( 'the_posts', 'sticky_count_offset', 10, 2 );
+function sticky_count_offset( $posts, $q ) {
+    if ( $q->get( 'ppp_include_sticky' )
+      && 0 < $q->get( 'posts_per_page' )
+      && false == $q->get( 'ignore_sticky_posts' ) ) {
+        $posts = array_slice( $posts, 0, $q->get( 'posts_per_page' ) );
+    }
+    return $posts;
+}
+
+function is_paged_home_with_sticky( $q ) {
+    return $q->is_home()
+      && $q->get( 'ppp_include_sticky' )
+      && $q->is_paged()
+      && false == $q->get( 'ignore_sticky_posts' );
+}
+
+add_filter( 'pre_get_posts', 'sticky_count_on_paged', 20 );
+function sticky_count_on_paged( $q ) {
+    if ( is_paged_home_with_sticky( $q ) ) {
+        $q->set('post__not_in', get_option( 'sticky_posts', array() ) );
+        $offset = count( get_option( 'sticky_posts', array() ) );
+        $page_offset = $q->get( 'posts_per_page', get_option( 'posts_per_page' ) ) * ( $q->get( 'paged' ) -1 ) - $offset;
+        $q->set( 'offset', $page_offset );
+    }
+    return $q;
+}
+
+add_filter( 'found_posts', 'sticky_adjust_offset_pagination', 1, 2 );
+function sticky_adjust_offset_pagination( $found_posts, $q ) {
+    if ( is_paged_home_with_sticky( $q ) ) {
+        $offset = count( get_option( 'sticky_posts', array() ) );
+        return $found_posts + $offset;
+    }
+    return $found_posts;
+}

File sticky.php Deleted

  • Ignore whitespace
  • Hide word diff
-<?php
-
-add_filter( 'query_vars', 'sticky_offset_query_var' );
-function sticky_offset_query_var( $vars ) {
-    $vars[] = 'ppp_include_sticky';
-    return $vars;
-}
-
-add_filter( 'pre_get_posts', 'w_sticky_posts' );
-function w_sticky_posts($q) {
-    if ( is_home() ) {
-        $q->set('ppp_include_sticky',true);
-    }
-    return $q;
-}
-
-add_filter( 'the_posts', 'sticky_count_offset', 10, 2 );
-function sticky_count_offset( $posts, $q ) {
-    if ( $q->get( 'ppp_include_sticky' )
-      && 0 < $q->get( 'posts_per_page' )
-      && false == $q->get( 'ignore_sticky_posts' ) ) {
-        $posts = array_slice( $posts, 0, $q->get( 'posts_per_page' ) );
-    }
-    return $posts;
-}
-
-function is_paged_home_with_sticky( $q ) {
-    return $q->is_home()
-      && $q->get( 'ppp_include_sticky' )
-      && $q->is_paged()
-      && false == $q->get( 'ignore_sticky_posts' );
-}
-
-add_filter( 'pre_get_posts', 'sticky_count_on_paged', 20 );
-function sticky_count_on_paged( $q ) {
-    if ( is_paged_home_with_sticky( $q ) ) {
-        $q->set('post__not_in', get_option( 'sticky_posts', array() ) );
-        $offset = count( get_option( 'sticky_posts', array() ) );
-        $page_offset = $q->get( 'posts_per_page', get_option( 'posts_per_page' ) ) * ( $q->get( 'paged' ) -1 ) - $offset;
-        $q->set( 'offset', $page_offset );
-    }
-    return $q;
-}
-
-add_filter( 'found_posts', 'sticky_adjust_offset_pagination', 1, 2 );
-function sticky_adjust_offset_pagination( $found_posts, $q ) {
-    if ( is_paged_home_with_sticky( $q ) ) {
-        $offset = count( get_option( 'sticky_posts', array() ) );
-        return $found_posts + $offset;
-    }
-    return $found_posts;
-}

File sync-sticky-values-with-acf.php Modified

  • Ignore whitespace
  • Hide word diff
 <?php
 
+// If ACF field `sticky_posts` is on an option page
 add_filter( 'pre_option_options_sticky_posts', 'w_get_sticky' );
 function w_get_sticky() {
     return get_option( 'sticky_posts' );
     }
 }
 
+// If ACF field `sticky_posts` is a page_for_posts metabox
 add_filter( 'get_post_metadata', 'w_get_sticky_post_meta', 10, 3 );
 function w_get_sticky_post_meta( $check, $object_id, $meta_key ) {
     if ( get_option( 'page_for_posts' ) == $object_id
  1. 1
  2. 2
  3. 3
  4. 4
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.