我想将自定义帖子类型的默认数据库从 wp_posts 更改为 wp_anything_else,我需要拥有仅由某些帖子获取的唯一 ID。例如,如果我有帖子类型博客,它也会将其添加到 wp_posts 中,但我不希望这样,它会把一切搞乱:( 在functions.php或任何插件中创建自定义帖子类型时,有没有办法更改数据库?
*来自functions.php的代码
add_filter('body_class','bp_conditional_ie_classes');
/*custom post types*/
add_action('init', 'blogs_register');
function blogs_register() {
$labels = array(
'name' => _x('My Blog', 'post type general name'),
'singular_name' => _x('Blog Item', 'post type singular name'),
'add_new' => _x('Add New', 'Blog item'),
'add_new_item' => __('Add New Blog Item'),
'edit_item' => __('Edit Blog Item'),
'new_item' => __('New Blog Item'),
'view_item' => __('View Blog Item'),
'search_items' => __('Search Blog'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => get_stylesheet_directory_uri() . '/admin.png',
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','thumbnail')
);
register_post_type( 'blogs' , $args );
}
**snippet 不起作用,但这是它允许我添加代码而不弄乱它的唯一方法
最佳答案
WordPress 帖子(帖子、页面、自定义帖子等)旨在代表您网站的一个实体。由于这种抽象,WordPress 使用一张表来存储其所有内容类型,因此在平均站点请求期间,WP 必须仅在一张表中查找所请求的站点。
在多个表中拥有多种帖子类型将需要在 WP 内核中采用不同的查找行为。这就是为什么你想要的事情做起来很复杂。
尽管我确信“博客项目”不需要单独的数据库表,但我在 wp_posts
表中看到的问题是它们不启用 m:n
-其条目之间的关系。对于这样的用例,您可以考虑使用自定义帖子和自定义分类法的组合。
编辑:当然,您可以编写或使用(如果存在)一个插件来处理管理和维护附加表所需的所有内容。但这可能永远不会集成到 WordPress 以及自定义帖子类型中。
关于php - 更改 WordPress SQL 数据库以自定义帖子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29473166/