WP_DROPDOWN_PAGES不允许我选择多个自定义帖子类型

时间:2016-02-16 作者:eve_mf

我正在创建一个“葡萄酒数据库”,称之为“葡萄酒数据库”。。。。基本上,有一个定制的帖子类型叫做“葡萄酒”,然后它有葡萄酒、葡萄色等属性。。。。。还有“地区”和“生产者”。我创建了最后两个自定义帖子类型,因为用户需要能够通过仪表板创建“producer”和“region”页面/帖子,基本上,他们需要的元素与创建新帖子时相同。

我不允许使用任何插件(付费或非付费)或任何主题,因此我必须对所有内容进行硬编码。

事实是。。。。我曾尝试将它们创建为分类法,但后来我无法确定如何通过仪表板添加帖子或页面的所有功能,因为它只包含标题、slug和描述。。。

好吧,现在我的问题是,我已经创建了带有下拉列表的元框,所以当您创建新葡萄酒时,您可以在元框中选择区域和生产者。问题它只保存生产者:S一旦我选择了区域,并尝试更新,所选选项将再次变为未选中。

有什么关于为什么会这样的建议吗?如果wp\\u dropdown\\u页面出现问题,因为这是唯一让我觉得可能是错误的东西。。。。有没有其他建议?

非常感谢。

因此,我在这里注册了我的自定义帖子类型:

add_action(\'init\', function(){
    $labels = array(
        "name" => "Producers",
        "singular_name" => "Producer",
        "menu_name" => "Producers",
        "all_items" => "All Producers",
        "add_new" => "Add New",
        "add_new_item" => "Add New Producers",
        "edit" => "Edit",
        "edit_item" => "Edit Producer",
        "new_item" => "New Producer",
        "view" => "View",
        "view_item" => "View Producer",
        "search_items" => "Search Producers",
        "not_found" => "No Producers Found",
        "not_found_in_trash" => "No Producers Found in Trash",
        "parent" => "Parent Producers",
    );

    $args = array(
        "labels" => $labels,
         "description" => "",
        "public" => true,
        "show_ui" => true,
        "has_archive" => true,
        "show_in_menu" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => true,
        "rewrite" => array( "slug" => "producers", "with_front" => true ),
        "query_var" => true,
        "supports" => array(
           "editor",
            "title",
            "thumbnail",
            "revisions",
            "custom-fields",
            "page-attributes",   
        )
    );

    register_post_type( "producer", $args );

    $labels = array(
        "name" => "Wines",
        "singular_name" => "Wine",
    );

    $args = array(
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "show_ui" => true,
        "has_archive" => true,
        "show_in_menu" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => array( "slug" => "wines", "with_front" => true ),
        "query_var" => true,
        "supports" => array(  
            "editor",
            "title",
            "thumbnail",
            "revisions",
            "custom-fields",
            "page-attributes",
        ), 
    );

    register_post_type( "wines", $args );

       $labels = array(
        "name" => "Regions",
        "singular_name" => "Region",
        "menu_name" => "Regions",
        "all_items" => "All Regions",
        "add_new" => "Add New",
        "add_new_item" => "Add Region",
        "edit" => "Edit",
        "edit_item" => "Edit Region",
        "new_item" => "Region",
        "view" => "View",
        "view_item" => "View Region",
        "search_items" => "Search Regions",
        "not_found" => "No Region Found",
        "not_found_in_trash" => "No Region Found in Trash",
        "parent" => "Parent Country",
    );

    $args = array(
        "labels" => $labels,
         "description" => "",
        "public" => true,
        "show_ui" => true,
        "has_archive" => true,
        "show_in_menu" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => true,
        "rewrite" => array( "slug" => "regions", "with_front" => true ),
        "query_var" => true,
        "supports" => array(
           "editor",
            "title",
            "thumbnail",
            "revisions",
            "custom-fields",
            "page-attributes",   
        )
    );

    register_post_type( "region", $args );
});
在这里,我使用下拉列表注册元框:

add_action(\'add_meta_boxes\', function() {
    add_meta_box(\'wines2-parent\', \'regions\', \'wines_attributes_meta_box2\', \'wines\', \'side\', \'default\');
});

function wines_attributes_meta_box2($post) {
        $pages = wp_dropdown_pages(array(\'post_type\' => \'region\', \'selected\' => $post->post_parent, \'name\' => \'parent_id\', \'show_option_none\' => __(\'(no parent)\'), \'sort_column\'=> \'menu_order, post_title\', \'echo\' => 0));
        if ( ! empty($pages) ) {
            echo $pages;
        } // end empty pages check
}
add_action(\'add_meta_boxes\', function() {
    add_meta_box(\'wines-parent\', \'producers\', \'wines_attributes_meta_box\', \'wines\', \'side\', \'default\');
});

function wines_attributes_meta_box($post) {
        $pages = wp_dropdown_pages(array(\'post_type\' => \'producer\', \'selected\' => $post->post_parent, \'name\' => \'parent_id\', \'show_option_none\' => __(\'(no parent)\'), \'sort_column\'=> \'menu_order, post_title\', \'echo\' => 0));
        if ( ! empty($pages) ) {
            echo $pages;
        } // end empty pages check
}

1 个回复
最合适的回答,由SO网友:birgire 整理而成

问题它只保存生产者:S一旦我选择了区域,并尝试更新,所选选项将再次变为未选中。

潜在的问题似乎是您在编辑页面上添加了两个选择框same name attribute, 即parent_id.

请注意,这也是页面属性元框中默认父选择框使用的相同名称属性。

这个parent_id 存储在wp_posts 表,该字段为unsigned bigint(20). 所以这只能存储一个整数。

您应该使用两个不同的名称属性,例如:

wp_dropdown_pages( [ \'name\' => \'wpse_producer_id\', ...

wp_dropdown_pages( [ \'name\' => \'wpse_region_id\', ...
在前面加上前缀wpse_ 例如,为了避免可能的名称冲突。

然后,您可以在update_post_meta(). 这里有一些例子here 在法典中。

还必须在以下帮助下调整选定属性get_post_meta().

希望有帮助。

相关推荐

在WP搜索表单中是否有类似`wp_Dropdown_Categories()`的输入复选框选项?

我有自定义的帖子类型,当前正在使用wp_dropdown_categories() 在WP搜索表单中的功能,以便人们能够按薪资级别和工作类型进行筛选。是否有一个等效函数,以便用户可以使用输入复选框元素而不是<select> 元素,以便他们在进行搜索时可以选择多个工作类型或薪资水平?我似乎在开发者文档中找不到任何东西。我正在寻找的是允许复选框替换使用下面代码中的select选项的下拉元素的东西。<form method="get" action="<?p