Plugin Directory

Changeset 429287


Ignore:
Timestamp:
08/27/2011 01:08:31 AM (15 years ago)
Author:
superann
Message:

Modified filter method to control display of selected custom post types on the blog home.

Location:
sticky-custom-post-types/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sticky-custom-post-types/trunk/readme.txt

    r426819 r429287  
    77Stable tag: trunk
    88
    9 Enables a sticky checkbox on the custom post type add/edit page which will allow the user to stick custom post type entries to the front page.
     9Enables support for sticky custom post types.
    1010
    1111== Description ==
    1212
    13 This plugin enables sticky post support on custom post types by adding a "Stick this to the front page" checkbox on the admin add/edit page of selected custom post types.
     13This plugin adds a "Stick this to the front page" checkbox on the admin add/edit entry page of selected custom post types.
    1414
    15 Set custom post types to enable in Settings → Writing.
     15Set options to enable in Settings → Reading. Unless you're using custom queries to display your sticky posts, you probably want to check the option to add selected post types to the blog home.
     16
     17Note: Sticky custom posts are stored in the global 'sticky_posts' option field, just like regular sticky posts.
    1618
    1719== Installation ==
     
    19211. Upload `sticky-custom-post-types.php` to the `/wp-content/plugins/` directory.
    20222. Activate the plugin through the 'Plugins' menu in WordPress.
    21 3. Navigate to 'Settings → Writing' and select custom post types to enable.
     233. Navigate to 'Settings → Reading' and set your options.
    2224
    2325== Frequently Asked Questions ==
     
    3133== Changelog ==
    3234
     35= 1.2 =
     36* Modified filter method to control display of selected custom post types on the blog home, and added an option to allow the user to enable/disable the filter.
     37* Moved plugin settings from 'Settings → Writing' to 'Settings → Reading'.
     38
    3339= 1.1 =
    3440* Moved plugin settings from 'Settings → General' to 'Settings → Writing'.
  • sticky-custom-post-types/trunk/sticky-custom-post-types.php

    r426804 r429287  
    33Plugin Name: Sticky Custom Post Types
    44Plugin URI: http://superann.com/sticky-custom-post-types/
    5 Description: Enables a sticky checkbox on the admin add/edit page of custom post types which will allow the user to stick custom post type entries to the front page. Select custom post types in Settings → Writing.
    6 Version: 1.1
     5Description: Enables support for sticky custom post types. Set options in Settings → Reading.
     6Version: 1.2
    77Author: Ann Oyama
    88Author URI: http://superann.com
     
    2727
    2828function super_sticky_description() {
    29     echo '<p>'.__('Enable selected custom post types to stick to the front page.').'</p>';
     29    echo '<p>'.__('Enable support for sticky custom post types.').'</p>';
    3030}
    3131
    32 function super_sticky_settings() {
     32function super_sticky_set_post_types() {
    3333    $post_types = get_post_types(array('_builtin' => false, 'public' => true), 'names');
    3434    if(!empty($post_types)) {
    35         $checked_post_types = super_sticky_get();
     35        $checked_post_types = super_sticky_post_types();
    3636        foreach($post_types as $post_type) { ?>
    37             <span><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></span><br/><?php
     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><?php
    3838        }
    3939    }
     
    4242}
    4343
     44function 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}
     49
     50function 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 page</label></span><?php
     52}
     53
    4454function super_sticky_admin_init() {
    45     register_setting('writing', 'sticky_custom_post_types');
    46     add_settings_section('super_sticky_options', 'Sticky Custom Post Types', 'super_sticky_description', 'writing');
    47     add_settings_field('sticky_custom_post_types', 'Custom Post Types', 'super_sticky_settings', 'writing', 'super_sticky_options');
     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');
    4860}
    4961
    5062add_action('admin_init', 'super_sticky_admin_init', 20);
    5163
    52 function super_sticky_get($posts=FALSE) {
     64function super_sticky_post_types($posts=false) {
    5365    $post_types = get_option('sticky_custom_post_types');
    5466    if(!is_array($post_types))
     
    5971}
    6072
    61 function super_sticky_meta() { ?>
     73function super_sticky_meta() { global $post; ?>
    6274    <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><?php
    6375}
    6476
    6577function super_sticky_add_meta_box() {
    66     foreach(super_sticky_get() as $post_type)
     78    foreach(super_sticky_post_types() as $post_type)
    6779        if(current_user_can('edit_others_posts'))
    6880            add_meta_box('super_sticky_meta', 'Sticky', 'super_sticky_meta', $post_type, 'side', 'high');
     
    7183add_action('admin_init', 'super_sticky_add_meta_box');
    7284
    73 function super_sticky_posts($query) {
    74     if(is_home() && (false === $query->query_vars['suppress_filters']))
    75         $query->set('post_type', super_sticky_get(TRUE));
     85function super_sticky_posts_filter($query) {
     86    if($query->is_home && !$query->is_paged)
     87        if(super_sticky_filter('home'))
     88            $query->set('post_type', super_sticky_post_types(true));
     89        else
     90            $query->set('post_type', 'post');
    7691    return $query;
    7792}
    7893
    79 add_filter('pre_get_posts', 'super_sticky_posts');
     94add_filter('pre_get_posts', 'super_sticky_posts_filter');
    8095?>
Note: See TracChangeset for help on using the changeset viewer.