自定义帖子类型不会输出帖子

时间:2019-05-24 02:43:32

标签: php wordpress custom-post-type

我正在创建一个插件,并且设置了一个自定义帖子类型,其中包括元框,自定义字段和自定义分类法。一切正常,在我的wordpress仪表板上,“添加新帖子”就在那里。但是,当我尝试“保存草稿”或“发布”帖子时,它不起作用。错误如下。

这是我的CPT的代码。

 // Register Custom Post Type
function post_type_book() {

    $labels = array(
        'name'                  => _x( 'All Books', 'book post type name', 'author_station' ),
        'singular_name'         => _x( 'Book', 'Singular book post type name', 'author_station' ),
        'menu_name'             => __( 'Books', 'author_station' ),
        'name_admin_bar'        => __( 'Post Type', 'author_station' ),
        'archives'              => __( 'Item Archives', 'author_station' ),
        'attributes'            => __( 'Item Attributes', 'author_station' ),
        'parent_item_colon'     => __( 'Parent Item:', 'author_station' ),
        'all_items'             => __( 'All Items', 'author_station' ),
        'add_new_item'          => __( 'Add New Book', 'author_station' ),
        'add_new'               => __( 'Add New', 'author_station' ),
        'new_item'              => __( 'New Book', 'author_station' ),
        'edit_item'             => __( 'Edit Book', 'author_station' ),
        'update_item'           => __( 'Update Item', 'author_station' ),
        'view_item'             => __( 'View Book', 'author_station' ),
        'view_items'            => __( 'View Items', 'author_station' ),
        'search_items'          => __( 'Search Item', 'author_station' ),
        'not_found'             => __( 'Not books found', 'author_station' ),
        'not_found_in_trash'    => __( 'Not books found in Trash', 'author_station' ),
        'featured_image'        => __( 'Book Cover Image', 'author_station' ),
        'set_featured_image'    => __( 'Set Book Cover Image', 'author_station' ),
        'remove_featured_image' => __( 'Remove Book Cover Image', 'author_station' ),
        'use_featured_image'    => __( 'Use as Book Cover Image', 'author_station' ),
        'insert_into_item'      => __( 'Insert into item', 'author_station' ),
        'uploaded_to_this_item' => __( 'Uploaded to this item', 'author_station' ),
        'items_list'            => __( 'Items list', 'author_station' ),
        'items_list_navigation' => __( 'Items list navigation', 'author_station' ),
        'filter_items_list'     => __( 'Filter items list', 'author_station' ),
    );

    $args = array(
        'label'                 => __( 'book', 'author_station' ),
        'description'           => __( 'Custom Book Entry', 'author_station' ),
        'supports'              => array('title'),
        'taxonomies'            => array( 'genres', 'series', 'keywords' ),
        'labels'                => $labels,
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => false,
        'menu_position'         => 20,
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'post',
        'rewrite' => array( 'slug' => 'as_book' ),

    );
    register_post_type( 'as_book', $args );

}
add_action( 'init', 'post_type_book');







        function save_as_book( $post_id ) {
        // verify nonce
        if ( !wp_verify_nonce( $_POST['your_as_book_nonce'], basename(__FILE__) ) ) {
            return $post_id;
        }
        // check autosave
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return $post_id;
        }
        // check permissions
        if ( 'page' === $_POST['as_book'] ) {
            if ( !current_user_can( 'edit_page', $post_id ) ) {
                return $post_id;
            } elseif ( !current_user_can( 'edit_post', $post_id ) ) {
                return $post_id;
            }
        }

        $old = get_post_meta( $post_id, 'as_book', true );
        $new = $_POST['as_book'];

        if ( $new && $new !== $old ) {
            update_post_meta( $post_id, 'as_book', $new );
        } elseif ( '' === $new && $old ) {
            delete_post_meta( $post_id, 'as_book', $old );
        }
    } 
    add_action( 'save_post', 'save_as_book' );

这是我尝试“保存草稿”或“发布”帖子时出现的错误。

Fatal error: Call to a member function get_queried_object_id() on null in ..../wp-includes/query.php on line 60

0 个答案:

没有答案