Changeset 605669
- Timestamp:
- 09/29/2012 07:57:20 AM (13 years ago)
- Location:
- sticky-custom-post-types/trunk
- Files:
-
- 2 edited
-
readme.txt (modified) (3 diffs)
-
sticky-custom-post-types.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sticky-custom-post-types/trunk/readme.txt
r435042 r605669 4 4 Tags: custom post types, sticky 5 5 Requires at least: 3.0 6 Tested up to: 3. 2.16 Tested up to: 3.4.2 7 7 Stable tag: trunk 8 License: GPLv2 or later 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html 8 10 9 11 Enables support for sticky custom post types. … … 33 35 == Changelog == 34 36 37 = 1.2.3 = 38 * Added contributions by [Viper007Bond](http://www.viper007bond.com) including enhanced posts filter (now affects main query only and will respect other post types) and formatting updates to conform with the official WordPress Coding Standards doc in the codex. 39 * Home query no longer modified at all if "Display selected post type(s) on home page" option is left unchecked (note that this means that checked sticky custom posts will show up in the home feed but non-sticky custom posts will be left out of the normal flow). 40 35 41 = 1.2.2 = 36 42 * Added custom post types to paged blog home. … … 48 54 = 1.0 = 49 55 * Initial version. 56 57 == Upgrade Notice == 58 59 = 1.2.3 = 60 Activating the posts filter function on home now affects main query only and respects other post types, which should make it play more nicely with other plugins. -
sticky-custom-post-types/trunk/sticky-custom-post-types.php
r435040 r605669 4 4 Plugin URI: http://superann.com/sticky-custom-post-types/ 5 5 Description: Enables support for sticky custom post types. Set options in Settings → Reading. 6 Version: 1.2. 26 Version: 1.2.3 7 7 Author: Ann Oyama 8 8 Author URI: http://superann.com … … 26 26 */ 27 27 28 add_action( 'admin_init', 'super_sticky_add_meta_box' ); 29 add_action( 'admin_init', 'super_sticky_admin_init', 20 ); 30 add_action( 'pre_get_posts', 'super_sticky_posts_filter' ); 31 28 32 function super_sticky_description() { 29 echo '<p>' .__('Enable support for sticky custom post types.').'</p>';33 echo '<p>' . __( 'Enable support for sticky custom post types.' ) . '</p>'; 30 34 } 31 35 32 36 function super_sticky_set_post_types() { 33 $post_types = get_post_types( array('_builtin' => false, 'public' => true), 'names');34 if (!empty($post_types)) {37 $post_types = get_post_types( array( '_builtin' => false, 'public' => true ), 'names' ); 38 if ( ! empty( $post_types ) ) { 35 39 $checked_post_types = super_sticky_post_types(); 36 foreach ($post_types as $post_type) { ?>37 <div><input type="checkbox" id=" post_type_<?php echo $post_type; ?>" name="sticky_custom_post_types[]" value="<?php echo $post_type; ?>" <?php checked(in_array($post_type, $checked_post_types)); ?> /> <label for="post_type_<?php echo $post_type; ?>"><?php echo $post_type; ?></label></div><?php40 foreach ( $post_types as $post_type ) { ?> 41 <div><input type="checkbox" id="<?php echo esc_attr( 'post_type_' . $post_type ); ?>" name="sticky_custom_post_types[]" value="<?php echo esc_attr( $post_type ); ?>" <?php checked( in_array( $post_type, $checked_post_types ) ); ?> /> <label for="<?php echo esc_attr( 'post_type_' . $post_type ); ?>"><?php echo esc_html( $post_type ); ?></label></div><?php 38 42 } 43 } else { 44 echo '<p>' . __( 'No public custom post types found.' ) . '</p>'; 39 45 } 40 else41 echo '<p>'.__('No public custom post types found.').'</p>';42 46 } 43 47 44 function super_sticky_filter( $query_type) {45 $filters = get_option('sticky_custom_post_types_filters');46 if(!is_array($filters)) $filters = array(); 47 return in_array($query_type, $filters);48 function super_sticky_filter( $query_type ) { 49 $filters = (array) get_option( 'sticky_custom_post_types_filters', array() ); 50 51 return in_array( $query_type, $filters ); 48 52 } 49 53 50 54 function super_sticky_set_filters() { ?> 51 <span><input type="checkbox" id="sticky_custom_post_types_filters_home" name="sticky_custom_post_types_filters[]" value="home" <?php checked( super_sticky_filter('home')); ?> /> <label for="sticky_custom_post_types_filters_home">home</label></span><?php55 <span><input type="checkbox" id="sticky_custom_post_types_filters_home" name="sticky_custom_post_types_filters[]" value="home" <?php checked( super_sticky_filter( 'home' ) ); ?> /> <label for="sticky_custom_post_types_filters_home">home</label></span><?php 52 56 } 53 57 54 58 function super_sticky_admin_init() { 55 register_setting( 'reading', 'sticky_custom_post_types');56 register_setting( 'reading', 'sticky_custom_post_types_filters');57 add_settings_section( 'super_sticky_options', 'Sticky Custom Post Types', 'super_sticky_description', 'reading');58 add_settings_field( 'sticky_custom_post_types', 'Show "Stick this..." checkbox on', 'super_sticky_set_post_types', 'reading', 'super_sticky_options');59 add_settings_field( 'sticky_custom_post_types_filters', 'Display selected post type(s) on', 'super_sticky_set_filters', 'reading', 'super_sticky_options');59 register_setting( 'reading', 'sticky_custom_post_types' ); 60 register_setting( 'reading', 'sticky_custom_post_types_filters' ); 61 add_settings_section( 'super_sticky_options', __( 'Sticky Custom Post Types' ), 'super_sticky_description', 'reading' ); 62 add_settings_field( 'sticky_custom_post_types', __( 'Show "Stick this..." checkbox on' ), 'super_sticky_set_post_types', 'reading', 'super_sticky_options' ); 63 add_settings_field( 'sticky_custom_post_types_filters', __( 'Display selected post type(s) on' ), 'super_sticky_set_filters', 'reading', 'super_sticky_options' ); 60 64 } 61 65 62 add_action('admin_init', 'super_sticky_admin_init', 20); 63 64 function super_sticky_post_types($posts=false) { 65 $post_types = get_option('sticky_custom_post_types'); 66 if(!is_array($post_types)) 67 $post_types = array(); 68 if($posts) 69 $post_types[] = 'post'; 70 return $post_types; 66 function super_sticky_post_types() { 67 return (array) get_option( 'sticky_custom_post_types', array() ); 71 68 } 72 69 73 function super_sticky_meta() { global $post;?>74 <input id="super-sticky" name="sticky" type="checkbox" value="sticky" <?php checked( is_sticky($post->ID)); ?> /> <label for="super-sticky" class="selectit"><?php _e('Stick this to the front page') ?></label><?php70 function super_sticky_meta() { ?> 71 <input id="super-sticky" name="sticky" type="checkbox" value="sticky" <?php checked( is_sticky() ); ?> /> <label for="super-sticky" class="selectit"><?php _e( 'Stick this to the front page' ) ?></label><?php 75 72 } 76 73 77 74 function super_sticky_add_meta_box() { 78 foreach(super_sticky_post_types() as $post_type) 79 if(current_user_can('edit_others_posts')) 80 add_meta_box('super_sticky_meta', 'Sticky', 'super_sticky_meta', $post_type, 'side', 'high'); 75 if( ! current_user_can( 'edit_others_posts' ) ) 76 return; 77 78 foreach( super_sticky_post_types() as $post_type ) 79 add_meta_box( 'super_sticky_meta', __( 'Sticky' ), 'super_sticky_meta', $post_type, 'side', 'high' ); 81 80 } 82 81 83 add_action('admin_init', 'super_sticky_add_meta_box'); 82 function super_sticky_posts_filter( $query ) { 83 if ( $query->is_main_query() && $query->is_home() && ! $query->get( 'suppress_filters' ) && super_sticky_filter( 'home' ) ) { 84 84 85 function super_sticky_posts_filter($query) { 86 if($query->is_home && !$query->get('suppress_filters')) 87 if(super_sticky_filter('home')) 88 $query->set('post_type', super_sticky_post_types(true)); 89 else 90 $query->set('post_type', 'post'); 91 return $query; 85 $super_sticky_post_types = super_sticky_post_types(); 86 87 if ( ! empty( $super_sticky_post_types ) ) { 88 $post_types = array(); 89 90 $query_post_type = $query->get( 'post_type' ); 91 92 if ( empty( $query_post_type ) ) { 93 $post_types[] = 'post'; 94 } elseif ( is_string( $query_post_type ) ) { 95 $post_types[] = $query_post_type; 96 } elseif ( is_array( $query_post_type ) ) { 97 $post_types = $query_post_type; 98 } else { 99 return; // Unexpected value 100 } 101 102 $post_types = array_merge( $post_types, $super_sticky_post_types ); 103 104 $query->set( 'post_type', $post_types ); 105 } 106 } 92 107 } 93 108 94 add_filter('pre_get_posts', 'super_sticky_posts_filter');95 109 ?>
Note: See TracChangeset
for help on using the changeset viewer.