I have created a custom post type and added some custom metaboxes to the page edit screen. My objective was to provide specific metaboxes which a user can edit which in turn would allow a user to modify specific content areas of a websites homepage.

Everything described above works perfectly except that I seem to be unable to set the page created from this custom post type as the “static page” within the wordpress admin “settings” area.

It seems the dropdown which lets your select a static page only shows pages located within the default wordpress “page” post_type.

How would I be able to change these settings to achieve my goal?

4 Answers
4

Add a filter for 'wp_dropdown_pages'. If 'name' => 'page_on_front', fill the list with your custom post types.

Here is a very primitive example as plugin. I took the custom post type from the codex.

<?php
/*
Plugin Name: Custom Post Type as Front Page
Description: Adds your custom post type to the list of available front pages.
Version:     0.1
Author:      Thomas Scholz
Author URI:  http://toscho.de
License:     GPL v2
*/

add_action('init', 'my_custom_init');
function my_custom_init()
{
    $labels = array(
    'name' => _x('Books', 'post type general name'),
    'singular_name' => _x('Book', 'post type singular name'),
    'add_new' => _x('Add New', 'book'),
    'add_new_item' => __('Add New Book'),
    'edit_item' => __('Edit Book'),
    'new_item' => __('New Book'),
    'view_item' => __('View Book'),
    'search_items' => __('Search Books'),
    'not_found' =>  __('No books found'),
    'not_found_in_trash' => __('No books found in Trash'),
    'parent_item_colon' => ''
    );
    $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array('title','editor','author','thumbnail','excerpt','comments')
    );
    register_post_type('book',$args);
}

//add filter to insure the text Book, or book, is displayed when user updates a book
add_filter('post_updated_messages', 'book_updated_messages');
function book_updated_messages( $messages ) {
    global $post, $post_ID;

    $messages['book'] = array(
    0 => '', // Unused. Messages start at index 1.
    1 => sprintf( __('Book updated. <a href="https://wordpress.stackexchange.com/questions/3927/%s">View book</a>'), esc_url( get_permalink($post_ID) ) ),
    2 => __('Custom field updated.'),
    3 => __('Custom field deleted.'),
    4 => __('Book updated.'),
    /* translators: %s: date and time of the revision */
    5 => isset($_GET['revision']) ? sprintf( __('Book restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
    6 => sprintf( __('Book published. <a href="https://wordpress.stackexchange.com/questions/3927/%s">View book</a>'), esc_url( get_permalink($post_ID) ) ),
    7 => __('Book saved.'),
    8 => sprintf( __('Book submitted. <a target="_blank" href="https://wordpress.stackexchange.com/questions/3927/%s">Preview book</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
    9 => sprintf( __('Book scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview book</a>'),
    // translators: Publish box date format, see http://php.net/date
    date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
    10 => sprintf( __('Book draft updated. <a target="_blank" href="https://wordpress.stackexchange.com/questions/3927/%s">Preview book</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
    );

    return $messages;
}

// Here we go!
add_filter('wp_dropdown_pages', 'add_book_page_to_dropdown', 10, 1);

function add_book_page_to_dropdown( $select )
{
    // Not our list.
    if ( FALSE === strpos( $select, '<select name="page_on_front"' ) )
    {
        return $select;
    }

    $books = get_posts( array( 'post_type' => 'book' ) );

    if ( ! $books ) // no books found.
    {
        return $select;
    }

    $book_options = walk_page_dropdown_tree($books, 0,
         array(
            'depth' => 0
         ,  'child_of' => 0
         ,  'selected' => 0
         ,  'echo' => 0
         ,  'name' => 'page_on_front'
         ,  'id' => ''
         ,  'show_option_none' => ''
         ,  'show_option_no_change' => ''
         ,  'option_none_value' => ''
        )
    );

    return str_replace( '</select>', $book_options . '</select>', $select );
}

Activate, write a book page, go to Reading Settings, and choose that page as frontpage.

Screenshot

Hit Save Changes. Smile.

Leave a Reply

Your email address will not be published. Required fields are marked *