这只是一个起点,因为在进一步开发中肯定会出现问题。例如,现在,搜索功能中断,因为它期望一个字符串(post_type)并且正在接收一个数组。
要在“帖子”屏幕中列出一种以上的帖子类型,我们可以插入pre_get_posts
并修改查询。在此测试中,“帖子”,“页面”和“产品”将一起显示在“帖子”屏幕(http://example.com/wp-admin/edit.php
)中。
add_action( 'pre_get_posts', 'join_cpt_list_wspe_113808' );
function join_cpt_list_wspe_113808( $query )
{
// If not backend, bail out
if( !is_admin() )
return $query;
// Detect current page and list of CPTs to be shown in Dashboard > Posts > Edit screen
global $pagenow;
$cpts = array( 'post', 'page', 'product' );
if( 'edit.php' == $pagenow && ( get_query_var('post_type') && 'post' == get_query_var('post_type') ) )
$query->set( 'post_type', $cpts );
return $query;
}
一个帮助代码,用于显示每个帖子的帖子类型:
add_filter( 'manage_edit-post_columns', 'add_cpt_column_wspe_113808' );
foreach( array( 'post', 'page', 'product' ) as $cpt )
add_action( "manage_{$cpt}_posts_custom_column", 'show_cpt_column_wspe_113808', 10, 2 );
function add_cpt_column_wspe_113808( $columns )
{
$columns[ 'cpt' ] = 'Post Type';
return $columns;
}
function show_cpt_column_wspe_113808( $column_name, $post_id )
{
if ( 'cpt' != $column_name )
return;
echo get_post_type( $post_id );
}