{"id":41728,"date":"2014-08-24T21:57:34","date_gmt":"2014-08-24T21:57:34","guid":{"rendered":"https:\/\/br.wordpress.org\/support\/topic\/problemas-com-posts-personalizados-custom-post-types\/"},"modified":"2016-08-06T01:31:01","modified_gmt":"2016-08-06T01:31:01","slug":"problemas-com-posts-personalizados-custom-post-types","status":"closed","type":"topic","link":"https:\/\/br.wordpress.org\/support\/topic\/problemas-com-posts-personalizados-custom-post-types\/","title":{"rendered":"Problemas com posts personalizados (custom-post-types)"},"content":{"rendered":"<p>Ol\u00e1 a todos amigos entusiastas! Venho reportar aqui um problema que n\u00e3o estou conseguindo resolver sozinho e se algum dos senhores estiver interessado em ajudar-me, serei muito grato! Ent\u00e3o passemos o problema&#8230;<\/p>\n<p>Estou desenvolvendo um website para um corretor de im\u00f3veis e para melhorar a utiliza\u00e7\u00e3o do corretor, eu criei o tipo de post im\u00f3veis, que \u00e9 um custom_post_type. O problema est\u00e1 na a\u00e7\u00e3o de SAVE do post im\u00f3vel. Se eu n\u00e3o preencho todos os meta-boxes o post \u00e9 salvo com valores nulos nos meta-boxes. Deixo o c\u00f3digo abaixo para que possa ser analizado.<\/p>\n<p>O c\u00f3digo abaixo \u00e9 um php que anexo ao functions.php no final do c\u00f3digo.<br \/>\n<code><br \/>\n\/\/ Register Custom Post Type<br \/>\nfunction cadastro_imovel() {<\/p>\n<p>\t$labels = array(<br \/>\n\t\t'name'                =&gt; _x( 'Im\u00f3veis', 'Post Type General Name', 'imovel' ),<br \/>\n\t\t'singular_name'       =&gt; _x( 'Cadastrar im\u00f3vel', 'Post Type Singular Name', 'imovel' ),<br \/>\n\t\t'menu_name'           =&gt; __( 'Im\u00f3veis', 'imovel' ),<br \/>\n\t\t'parent_item_colon'   =&gt; __( 'Item refer\u00eancia:', 'imovel' ),<br \/>\n\t\t'all_items'           =&gt; __( 'Todos', 'imovel' ),<br \/>\n\t\t'view_item'           =&gt; __( 'Vizualizar', 'imovel' ),<br \/>\n\t\t'add_new_item'        =&gt; __( 'Cadastrar novo im\u00f3vel', 'imovel' ),<br \/>\n\t\t'add_new'             =&gt; __( 'Adicionar novo', 'imovel' ),<br \/>\n\t\t'edit_item'           =&gt; __( 'Editar', 'imovel' ),<br \/>\n\t\t'update_item'         =&gt; __( 'Atualizar', 'imovel' ),<br \/>\n\t\t'search_items'        =&gt; __( 'Buscar', 'imovel' ),<br \/>\n\t\t'not_found'           =&gt; __( 'N\u00e3o encontrado', 'imovel' ),<br \/>\n\t\t'not_found_in_trash'  =&gt; __( 'Nada encontrado na lixeira', 'imovel' ),<br \/>\n\t);<br \/>\n\t$args = array(<br \/>\n\t\t'label'               =&gt; __( 'imovel', 'imovel' ),<br \/>\n\t\t'description'         =&gt; __( 'Cadastro de im\u00f3vel', 'imovel' ),<br \/>\n\t\t'labels'              =&gt; $labels,<br \/>\n\t\t'supports'            =&gt; array( 'title', 'editor', 'author', 'thumbnail', ),<br \/>\n\t\t'hierarchical'        =&gt; false,<br \/>\n\t\t'public'              =&gt; true,<br \/>\n\t\t'show_ui'             =&gt; true,<br \/>\n\t\t'show_in_menu'        =&gt; true,<br \/>\n\t\t'show_in_nav_menus'   =&gt; true,<br \/>\n\t\t'show_in_admin_bar'   =&gt; true,<br \/>\n\t\t'menu_position'       =&gt; 6,<br \/>\n\t\t'can_export'          =&gt; true,<br \/>\n\t\t'has_archive'         =&gt; true,<br \/>\n\t\t'exclude_from_search' =&gt; false,<br \/>\n\t\t'publicly_queryable'  =&gt; true<br \/>\n\t);<br \/>\n\tregister_post_type( 'imovel', $args );<\/p>\n<p>}<\/p>\n<p>\/\/ Hook into the 'init' action<br \/>\nadd_action( 'init', 'cadastro_imovel', 0 );<br \/>\n\/\/ Register Custom Taxonomy<br \/>\nfunction localizacao_imovel() {<\/p>\n<p>\t$labels = array(<br \/>\n\t\t'name'                       =&gt; _x( 'Localidades', 'Taxonomy General Name', 'localidade-imovel' ),<br \/>\n\t\t'singular_name'              =&gt; _x( 'Localiza\u00e7\u00e3o do im\u00f3vel', 'Taxonomy Singular Name', 'localidade-imovel' ),<br \/>\n\t\t'menu_name'                  =&gt; __( 'Localidades', 'localidade-imovel' ),<br \/>\n\t\t'all_items'                  =&gt; __( 'Todas', 'localidade-imovel' ),<br \/>\n\t\t'parent_item'                =&gt; __( 'Localidade refer\u00eancia:', 'localidade-imovel' ),<br \/>\n\t\t'parent_item_colon'          =&gt; __( 'Item refer\u00eancia', 'localidade-imovel' ),<br \/>\n\t\t'new_item_name'              =&gt; __( 'Adicionar nova localidade', 'localidade-imovel' ),<br \/>\n\t\t'add_new_item'               =&gt; __( 'Adicionar nova', 'localidade-imovel' ),<br \/>\n\t\t'edit_item'                  =&gt; __( 'Editar', 'localidade-imovel' ),<br \/>\n\t\t'update_item'                =&gt; __( 'Atualizar', 'localidade-imovel' ),<br \/>\n\t\t'separate_items_with_commas' =&gt; __( 'Separe os itens com v\u00edrgulas', 'localidade-imovel' ),<br \/>\n\t\t'search_items'               =&gt; __( 'Buscar', 'localidade-imovel' ),<br \/>\n\t\t'add_or_remove_items'        =&gt; __( 'Adicionar ou remover item', 'localidade-imovel' ),<br \/>\n\t\t'choose_from_most_used'      =&gt; __( 'Escolher localidade mais utilizada', 'localidade-imovel' ),<br \/>\n\t\t'not_found'                  =&gt; __( 'N\u00e3o encontrado', 'localidade-imovel' ),<br \/>\n\t);<br \/>\n\t$args = array(<br \/>\n\t\t'labels'                     =&gt; $labels,<br \/>\n\t\t'hierarchical'               =&gt; true,<br \/>\n\t\t'public'                     =&gt; true,<br \/>\n\t\t'show_ui'                    =&gt; true,<br \/>\n\t\t'show_admin_column'          =&gt; true,<br \/>\n\t\t'show_in_nav_menus'          =&gt; true,<br \/>\n\t\t'show_tagcloud'              =&gt; true,<br \/>\n\t);<br \/>\n\tregister_taxonomy( 'localizacao', array( 'imovel' ), $args );<\/p>\n<p>}<\/p>\n<p>\/\/ Hook into the 'init' action<br \/>\nadd_action( 'init', 'localizacao_imovel', 0 );<\/p>\n<p>\/\/ Register Custom Taxonomy<br \/>\nfunction tags_imovel() {<\/p>\n<p>\t$labels = array(<br \/>\n\t\t'name'                       =&gt; _x( 'Tags de refer\u00eancia', 'Taxonomy General Name', 'tags-imovel' ),<br \/>\n\t\t'singular_name'              =&gt; _x( 'Tags de refer\u00eancia', 'Taxonomy Singular Name', 'tags-imovel' ),<br \/>\n\t\t'menu_name'                  =&gt; __( 'Tags de refer\u00eancia', 'tags-imovel' ),<br \/>\n\t\t'all_items'                  =&gt; __( 'Todas', 'tags-imovel' ),<br \/>\n\t\t'parent_item'                =&gt; __( 'Item refer\u00eancia', 'tags-imovel' ),<br \/>\n\t\t'parent_item_colon'          =&gt; __( 'Item refer\u00eancia:', 'tags-imovel' ),<br \/>\n\t\t'new_item_name'              =&gt; __( 'Adicionar nova refer\u00eancia', 'tags-imovel' ),<br \/>\n\t\t'add_new_item'               =&gt; __( 'Adicionar nova', 'tags-imovel' ),<br \/>\n\t\t'edit_item'                  =&gt; __( 'Editar', 'tags-imovel' ),<br \/>\n\t\t'update_item'                =&gt; __( 'Atualizar', 'tags-imovel' ),<br \/>\n\t\t'separate_items_with_commas' =&gt; __( 'Separe os itens usando v\u00edrgula', 'tags-imovel' ),<br \/>\n\t\t'search_items'               =&gt; __( 'Buscar', 'tags-imovel' ),<br \/>\n\t\t'add_or_remove_items'        =&gt; __( 'Adicionar ou remover itens', 'tags-imovel' ),<br \/>\n\t\t'choose_from_most_used'      =&gt; __( 'Escolher tag mais utilizada', 'tags-imovel' ),<br \/>\n\t\t'not_found'                  =&gt; __( 'Not Found', 'tags-imovel' ),<br \/>\n\t);<br \/>\n\t$args = array(<br \/>\n\t\t'labels'                     =&gt; $labels,<br \/>\n\t\t'hierarchical'               =&gt; true,<br \/>\n\t\t'public'                     =&gt; true,<br \/>\n\t\t'show_ui'                    =&gt; true,<br \/>\n\t\t'show_admin_column'          =&gt; true,<br \/>\n\t\t'show_in_nav_menus'          =&gt; true,<br \/>\n\t\t'show_tagcloud'              =&gt; true,<br \/>\n\t);<br \/>\n\tregister_taxonomy( 'tags', array( 'imovel' ), $args );<\/p>\n<p>}<\/p>\n<p>\/\/ Hook into the 'init' action<br \/>\nadd_action( 'init', 'tags_imovel', 0 );<br \/>\n\/\/ Add custom meta box<br \/>\nadd_action( 'add_meta_boxes', 'imovel_globalpos_box' );<br \/>\nfunction imovel_globalpos_box() {<br \/>\n    add_meta_box(<br \/>\n        'imovel_globalpos_box',<br \/>\n        __( 'Localiza\u00e7\u00e3o do im\u00f3vel (GPS)', 'imovel_globalpos_box' ),<br \/>\n        'imovel_globalpos_box_content',<br \/>\n        'imovel',<br \/>\n        'normal',<br \/>\n        'high'<br \/>\n    );<br \/>\n}<\/p>\n<p>function imovel_globalpos_box_content( $post ) {<br \/>\n  wp_nonce_field( plugin_basename( __FILE__ ), 'imovel_globalpos_box_content_nonce' );<br \/>\n  wp_enqueue_style( 'gmapspicker.css', get_template_directory_uri() . '\/inc\/gmapspicker\/jquery-gmaps-latlon-picker.css');<br \/>\n  wp_enqueue_script( 'gmapspicker', get_template_directory_uri() . '\/inc\/gmapspicker\/jquery-gmaps-latlon-picker.js' );<br \/>\n  wp_enqueue_script( 'gmapsapi', 'http:\/\/maps.googleapis.com\/maps\/api\/js?sensor=false' );<br \/>\n  echo '&lt;label for=\"imovel_globalpos\"&gt;&lt;\/label&gt;';<br \/>\n  echo '&lt;fieldset class=\"gllpLatlonPicker\"&gt;';<br \/>\n  echo '&lt;div class=\"gllpMap\"&gt;Google Maps&lt;\/div&gt;';<br \/>\n  echo '&lt;input type=\"hidden\" id=\"imovel_globalpos_lat\" name=\"imovel_globalpos_lat\" class=\"gllpLatitude\"\/&gt;';<br \/>\n  echo '&lt;input type=\"hidden\" id=\"imovel_globalpos_long\" name=\"imovel_globalpos_long\" class=\"gllpLongitude\"\/&gt;';<br \/>\n  echo '&lt;\/fieldset&gt;';<br \/>\n}<br \/>\nadd_action( 'save_post', 'imovel_globalpos_box_save' );<\/p>\n<p>function imovel_globalpos_box_save( $post_id ) {<\/p>\n<p>  if ( defined( 'DOING_AUTOSAVE' ) &amp;&amp; DOING_AUTOSAVE )<br \/>\n  return;<\/p>\n<p>  if ( !wp_verify_nonce( $_POST['imovel_globalpos_box_content_nonce'], plugin_basename( __FILE__ ) ) )<br \/>\n  return;<\/p>\n<p>  if ( 'page' == $_POST['post_type'] ) {<br \/>\n    if ( !current_user_can( 'edit_page', $post_id ) )<br \/>\n    return;<br \/>\n  } else {<br \/>\n    if ( !current_user_can( 'edit_post', $post_id ) )<br \/>\n    return;<br \/>\n  }<br \/>\n  $imovel_globalpos_lat = $_POST['imovel_globalpos_lat'];<br \/>\n  update_post_meta( $post_id, 'imovel_globalpos_lat', $imovel_globalpos_lat );<br \/>\n  $imovel_globalpos_long = $_POST['imovel_globalpos_long'];<br \/>\n  update_post_meta( $post_id, 'imovel_globalpos_long', $imovel_globalpos_long );<br \/>\n}<br \/>\n<\/code><\/p>\n","protected":false},"template":"","class_list":["post-41728","topic","type-topic","status-closed","hentry","topic-tag-add_meta_box","topic-tag-custom-post-types","topic-tag-custom-fields","topic-tag-meta-box"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/br.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/41728","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/br.wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/br.wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/br.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/41728\/revisions"}],"wp:attachment":[{"href":"https:\/\/br.wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=41728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}