0

I am creating a "wine database",k to call it somehow.... Basically there is a custom post type called "wines", and then it has attributes as vintage, grape color..... and also, "region" and "producer". I have created those two last ones as custom post types because the user needs to be able to create a "producer" and "region" page/post through the dashboard, basically, they want the same elements than when you create a new post.

I am not allow to work with any plugin (paid or not) or any theme, so I have to hard code everything.

The fact is.... I have tried to create them as taxonomies, but then I couldn't work out how to add all the functionality a post or a page has trough the dashboard, as it only comes with title, slug, and description...

Well, now my problem is, I have created meta boxes with dropdowns, so when you are creating a new wine, you select the region and the producer in the meta box. Problem? it just save the producer :S as soon as I select the region, and I try to update, the selected option becomes unselected again.

Any suggestion about why is it happening? And in case there is something going on with wp_dropdown_pages, as it is the only thing makes me thing it can be wrong.... any other solution suggested?

Thank you!!

So here I register my custom post-types:

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 );
});

Here I register the metaboxes with the dropdowns:

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
}
0

Problem? it just save the producer :S as soon as I select the region, and I try to update, the selected option becomes unselected again.

The underlying problem seems to be that you're adding two select boxes on the edit page with the same name attribute, namely parent_id.

Note that this is also the same name attribute used by the default parent select box in the Page Attribute meta-box.

The parent_id is stored in the wp_posts table and that field is unsigned bigint(20). So this can only store a single integer.

You should use two different name attributes, like:

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

wp_dropdown_pages( [ 'name' => 'wpse_region_id', ...

where we prefixed it, with wpse_ as an example, to avoid possible name collisions.

Then you could store these values as post meta, for each post, with the help of update_post_meta(). There are some examples here in the Codex.

You would also have to adjust the selected attribute with the help of get_post_meta().

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.