<?php// Does the current user can define sticky posts for this custom post type?functionneed_sticky($post){$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'));}// Start deferring php outputadd_action('post_submitbox_minor_actions','w_post_submitbox_minor_actions');functionw_post_submitbox_minor_actions($post){if(need_sticky($post)){ob_start();}}// Get PHP output and print sticky posts metaboxadd_action('post_submitbox_misc_actions','w_post_submitbox_misc_actions');functionw_post_submitbox_misc_actions($post){if(need_sticky($post)){$temp=ob_get_clean();$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).' />';if(current_user_can('edit_others_posts')){$sticky_box.=sprintf('<span id="sticky-span"><input id="super-sticky" name="sticky" type="checkbox" value="sticky" %1$s /> <label for="super-sticky" class="selectit">%2$s</label><br/></span>',checked(is_sticky($post->ID),true,false),__('Stick this post to the front page'));}$re='/(<input.*id="visibility-radio-password")/U';$output=preg_replace($re,"{$sticky_box}$1",$temp);echo$output;}}
<?php// Add a custom query var to force post countadd_filter('query_vars','sticky_offset_query_var');functionsticky_offset_query_var($vars){$vars[]='ppp_include_sticky';return$vars;}// Add the query var on home page queryadd_filter('pre_get_posts','w_sticky_posts');functionw_sticky_posts($q){if(is_home()){$q->set('ppp_include_sticky',true);}return$q;}// Truncate posts array to adjust post countadd_filter('the_posts','sticky_count_offset',10,2);functionsticky_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;}// Are we on a paged home with sticky posts ?functionis_paged_home_with_sticky($q){return$q->is_home()&&$q->get('ppp_include_sticky')&&$q->is_paged()&&false==$q->get('ignore_sticky_posts');}// Define page offset to show posts truncated on previous pageadd_filter('pre_get_posts','sticky_count_on_paged',20);functionsticky_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;}// Adjust found_posts property to display the right number of pagesadd_filter('found_posts','sticky_adjust_offset_pagination',1,2);functionsticky_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;}
<?php/** * If ACF field `sticky_posts` is on an option page */add_filter('pre_option_options_sticky_posts','w_get_sticky');functionw_get_sticky(){returnget_option('sticky_posts');}add_action('update_option','w_update_sticky',10,3);functionw_update_sticky($option,$old_value,$value){if('options_sticky_posts'===$option){update_option('sticky_posts',$value);}}/** * If ACF field `sticky_posts` is a page_for_posts metabox */add_filter('get_post_metadata','w_get_sticky_post_meta',10,3);functionw_get_sticky_post_meta($check,$object_id,$meta_key){if(get_option('page_for_posts')==$object_id&&'sticky_posts'===$meta_key){returnarray(get_option('sticky_posts'));}return$check;}add_action('updated_post_meta','w_update_sticky_post_meta',10,4);functionw_update_sticky_post_meta($meta_id,$object_id,$meta_key,$_meta_value){if(get_option('page_for_posts')==$object_id&&'sticky_posts'===$meta_key){update_option('sticky_posts',$_meta_value);}}
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.