【问题标题】:All posts and pages showing under custom post type admin menu在自定义帖子类型管理菜单下显示的所有帖子和页面
【发布时间】:2015-01-05 20:48:20
【问题描述】:

我在一个插件中为我的 wordpress 主题创建了一个自定义分类法分类法。激活插件后,我导航到自定义帖子类型的管理部分,该站点显示的所有帖子和页面。此外,当我尝试从自定义帖子类型管理区域中删除这些帖子和页面时,我收到“无效帖子类型”错误。 有没有人遇到过这种情况,有解决办法吗?

'code'add_action ('init', 'create_post_type' );

function create_post_type() {

register_post_type( 'my_slide',

    array(

        'labels' => array(
            'name' => __( 'Slides' ),
            'singular_name' => __( 'Slide' ),
            'add_new' => 'Add New',
            'add_new_item' => 'Add New Slide',
            'edit' => 'Edit',
            'edit_item' => 'Edit Slide',
            'new_item' => 'New Slide',
            'view' => 'View',
            'view_item' => 'View Slide',
            'search_items' => 'Search Slides',
            'not_found' => 'No Slides found',
            'not_found_in_trash' => 'No Slides found in Trash',
            'parent' => 'Parent Slide'
        ),

    'public' => true,
    'has_archive' => true,
    'show_ui' => true,  
    'capability_type' => 'post',  
    'hierarchical' => false,  
    'rewrite' => true,
    'map_meta_cap' => true,
    'query_var' => false,
    'register_meta_box_cb' => 'slide_meta_box_add',
    'supports' => array('title', 'editor', 'thumbnail', 'post-formats', 'Custom            Featured Image links')

    )
);



}

add_action( 'init', 'create_slider_taxonomies', 0 );

function create_slider_taxonomies() {
register_taxonomy(
    'slider_category',
array( 'my_slide' ),
    array(
        'labels' => array(
            'name' => 'Slide Category',
            'add_new_item' => 'Add New Slide Category',
            'new_item_name' => 'New Slide Category Name'
        ),
        'show_ui' => true,
        'show_tagcloud' => false,
    'show_admin_column' => true,
        'hierarchical' => true
    )
);

}'code'

【问题讨论】:

  • 显示你的插件代码。
  • @brasofilo - 我一遍又一遍地重复为什么它会表现得像这样,在我搜索过的任何论坛上都没有提及。我开始认为问题可能与服务器或数据库有关,除非您当然有任何其他建议。
  • 不,该代码没有任何问题。禁用所有其他插件并切换到默认主题,它仍然会发生吗?
  • 这是主题。您认为这是与查询相关的问题吗?
  • @Brasofilo 已解决 - pre_get_posts 过滤器

标签: wordpress custom-post-type


【解决方案1】:

正如您所说,问题在于pre_get_posts() 没有检查管理区域是否正在显示。这可以使用is_admin() 条件进行测试。

function add_post_types_to_query( $query ) {
    if ( (!is_admin()) && $query->is_main_query() )
        $query->set( 'post_type', array( 'post', 'video' ) ); //video is a custom post type
    return $query;
}
add_action( 'pre_get_posts', 'add_post_types_to_query' );

以上内容将停止帖子显示在所有管理区域中。

您可能希望检查另外几个条件。有了上述内容,我遇到了指向页面stoping working 的链接问题。这可以通过检查帖子页面is_archive() 来解决。有了这个,自定义帖子类型可能不会显示在主页上。这可以通过在函数中添加is_home() 条件来解决。

最终的 pre_get_posts 函数可能如下所示。

function add_post_types_to_query( $query ) {
    if ( (!is_admin()) && $query->is_main_query() )
        if ( $query->is_archive() || $query-> is_home() ) { 
            $query->set( 'post_type', array( 'post', 'video' ) ); //video is a custom post type
        }
    return $query;
}
add_action( 'pre_get_posts', 'add_post_types_to_query' );

我希望这对以后看到这篇文章的人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    • 2018-08-29
    • 2015-07-24
    • 2019-05-08
    • 2022-07-20
    相关资源
    最近更新 更多