Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've created the following custom post type (lugares means places) with a custom taxonomy of tacos and it works fine form the backend:

/**
*   Custom post-type lugares
**/

add_action('init', 'lugares_register');

function lugares_register() {

    $labels = array(
        'name' => _x('Lugares', 'post type general name'),
        'singular_name' => _x('Lugar', 'post type singular name'),
        'add_new' => _x('Agregar nuevo', 'lugar'),
        'add_new_item' => __('Agregar lugar'),
        'edit_item' => __('Editar lugar'),
        'new_item' => __('Nuevo lugar'),
        'view_item' => __('Ver lugar'),
        'search_items' => __('Buscar lugares'),
        'not_found' =>  __('No hay lugares'),
        'not_found_in_trash' => __('No hay lugares en la papelera'),
        'parent_item_colon' => ''
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title','editor','thumbnail','comments','custom-fields','revisions')
      );

    register_post_type( 'lugares' , $args );
}

register_taxonomy("tacos", array("lugares"), array("hierarchical" => true, "label" => "Tacos", "singular_label" => "Tacos", "rewrite" => true));

add_action("admin_init", "admin_init_lugares");

function admin_init_lugares(){
    add_meta_box("descripcion_usuario", "Descripción usuario", "descripcion_usuario", "lugares", "normal", "low");
    add_meta_box("direccion", "Dirección", "direccion", "lugares", "normal", "low");
    add_meta_box("horarios", "Horarios", "horarios", "lugares", "normal", "low");
    add_meta_box("telefonos", "Teléfonos", "telefonos", "lugares", "side", "low");
    add_meta_box("garantia_tdt", "Garantía TourDelTaco", "garantia_tdt", "lugares", "side", "low");
    add_meta_box("servicio_a_domicilio", "Servicio a domicilio", "servicio_a_domicilio", "lugares", "side", "low");
}

function descripcion_usuario() {
    global $post;
    $custom = get_post_custom($post->ID);
    $descripcion_usuario = ( !empty($custom["descripcion_usuario"][0]) ) ? $custom["descripcion_usuario"][0] : "";
    ?>
    <p><label>Descripción usuario:</label><br />
    <textarea cols="50" rows="5" name="descripcion_usuario"><?php echo $descripcion_usuario; ?></textarea></p>
    <?php
}

function direccion(){
    global $post;
    $custom = get_post_custom($post->ID);
    $direccion = ( !empty($custom["direccion"][0]) ) ? $custom["direccion"][0] : "";
    ?>
    <p><label>Direcci&oacute;n:</label><br />
    <textarea cols="50" rows="5" name="direccion"><?php echo $direccion; ?></textarea></p>
    <?php
}

function horarios() {
    global $post;
    $custom = get_post_custom($post->ID);
    $horarios = ( !empty($custom["horarios"][0]) ) ? $custom["horarios"][0]: "";
    ?>
    <p><label>Horarios:</label><br />
        <input type="text" name="horarios" value="<?php echo $horarios; ?>"/></p>
    <?php
}

function telefonos() {
    global $post;
    $custom = get_post_custom($post->ID);
    $telefonos = ( !empty($custom["telefonos"][0]) ) ? $custom["telefonos"][0] : "";
    ?>
    <p><label>Tel&eacute;fonos:</label><br />
    <textarea cols="38" rows="3" name="telefonos"><?php echo $telefonos; ?></textarea></p>
    <?php
}

function garantia_tdt() {
    global $post;
    $custom = get_post_custom($post->ID);
    $garantia_tdt = ( !empty($custom["garantia_tdt"][0]) ) ? $custom["garantia_tdt"][0] : "";
    ?>
    <p><label>Garant&iacute;a TourDelTaco:</label><br />
    <input type="radio" id="garantia_tdt" name="garantia_tdt" value="si" <?php if ($garantia_tdt == 'si') echo 'checked';?>/> Si<br />
    <input type="radio" id="garantia_tdt" name="garantia_tdt" value="no" <?php if ($garantia_tdt == 'no') echo 'checked';?>/> No
    </p>
    <?php
}

function servicio_a_domicilio() {
    global $post;
    $custom = get_post_custom($post->ID);
    $servicio_a_domicilio = (!empty($custom["servicio_a_domicilio"][0])) ? $custom["servicio_a_domicilio"][0] : "";
    ?>
    <p><label>&iquest;Cuenta con servicio a domicilio?:</label><br />
    <input type="radio" id="servicio_a_domicilio" name="servicio_a_domicilio" value="si" <?php if ($servicio_a_domicilio == 'si') echo 'checked';?>/> Si<br />
    <input type="radio" id="servicio_a_domicilio" name="servicio_a_domicilio" value="no" <?php if ($servicio_a_domicilio == 'no') echo 'checked';?>/> No
    </p>
    <?php
}

add_action('save_post', 'save_details_lugares');

function save_details_lugares(){
    global $post;
    $custom_meta_fields = array( 'horarios','direccion','descripcion_usuario','garantia_tdt','telefonos','servicio_a_domicilio' );

    foreach( $custom_meta_fields as $custom_meta_field ):
        if(isset($_POST[$custom_meta_field]) && $_POST[$custom_meta_field] != ""):
            update_post_meta($post->ID, $custom_meta_field, $_POST[$custom_meta_field]);
        endif;
    endforeach;

}

add_action("manage_posts_custom_column",  "lugares_custom_columns");
add_filter("manage_edit-lugares_columns", "lugares_edit_columns");

function lugares_custom_columns($column){
    global $post;

    switch ($column) {
        case "description":
            the_excerpt();
        break;
        case "direccion":
            $custom = get_post_custom();
            echo $custom["direccion"][0];
        break;
        case "horarios":
            $custom = get_post_custom();
            echo $custom["horarios"][0];
        break;
        case "descripcion_usuario":
            $custom = get_post_custom();
            echo $custom["descripcion_usuario"][0];
        break;
        case "tacos":
            echo get_the_term_list($post->ID, 'tacos', '', ', ','');
        break;
    }
}

function lugares_edit_columns($columns){
    $columns = array(
        "cb" => "<input type=\"checkbox\" />",
        "title" => "Nombre del lugar",
        "description" => "Descripci&oacute;n",
        "direccion" => "Direcci&oacute;n",
        "horarios" => "Horarios",
        "descripcion_usuario" => "Descripci&oacute;n usuario",
        "tacos" => "Tacos"
    );

    return $columns;
}

Adding new entries from a front-end form is partially working.

The code to the form is this:

<?php if ( empty($_POST) && !isset($_POST['agregar_lugar']) ) : ?>
    <h2>Agregar un lugar...</h2>
    <form id="" class="" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <label for="title">Nombre:</label><input type="text" name="title" id="title" />
    <label for="direccion">Direcci&oacute;n:</label><input type="text" name="direccion" id="direccion" />
    <label for="horarios">Horarios:</label><input type="text" name="horarios" id="horarios" />
    <label for="descripcion_usuario">Descripci&oacute;n:</label><input type="text" name="descripcion_usuario" id="descripcion_usuario" />
    <label for="servicio_a_domicilio">Servicio a domicilio:</label><input type="radio" name="servicio_a_domicilio" id="servicio_a_domicilio_si" value="si" />S&iacute;<input type="radio" name="servicio_a_domicilio" id="servicio_a_domicilio_no" value="no" />No
    <label for="telefonos">Tel&eacute;fonos:</label><input type="text" name="telefonos" id="telefonos" />

    <input type="submit" name="agregar_lugar" id="agregar-lugar" value="Agregar lugar">
    </form>
<?php else: 

    $title = $_POST['title'];
    $direccion = $_POST['direccion'];
    $horarios = $_POST['horarios'];
    $descripcion_usuario = $_POST['descripcion_usuario'];
    $servicio_a_domicilio = $_POST['servicio_a_domicilio'];
    $telefonos = $_POST['telefonos'];

    $errores = array();
    //title
    if(empty($title))
        $errores['title'] = 'Es obligatorio introducir un nombre.';

    //direccion
    if( empty($direccion) )
        $errores['direccion'] = 'Es obligatorio introducir una direcci&oacute;n.';

    if($servicio_a_domicilio !== 'si') $servicio_a_domicilio = 'no';

    // guardar
    $post = array(
        'post_title'    => $title,
        'post_status'   => 'draft',  
        'post_type' => 'Lugares',  
            'post_author'   => 2
    );

    $lugares_post_ID = wp_insert_post($post);
endif;

[And it just doesn't work. I could use some tips for debugging this because even with wp's debug set to true, I don't get a lot of information.]

Edit: it's partially working now. That is, posts are saved, but can't be edited from the backend.

I can't see what I'm doing wrong

share|improve this question
1  
can you try specifying post_author in the arguments? You will need global $user_id for this –  booota Sep 14 '11 at 5:34
    
thanks booota.. I haven't tried to do that. But you are right, maybe that's why it wont post. I'll try that and come back later. –  dabito Sep 14 '11 at 15:40
1  
best of luck. Hope you find it working :P –  booota Sep 15 '11 at 7:44
1  
what does it say when you edit? Also can you show the screenshot of your backend edit post page? –  booota Sep 21 '11 at 7:43
1  
1 thing more, can you tell me that which user has the ID of 2 and what is his role? –  booota Sep 21 '11 at 8:01
show 2 more comments

3 Answers

Have you tried TDO Mini Forms? It allows users posting content from the frontend. I don't know if it allows submitting custom types, but at least can give you a hint.

share|improve this answer
    
Thanks for the tip Flo, I will check TDO forms and see if it does what I want. –  dabito Sep 14 '11 at 15:38
add comment
up vote 0 down vote accepted

It turned out that there was a difference between the custom post type's name and the type I added in the form. One was "lugares" and the other one was "Lugares". From this, I asume capabilities were broken.

Rebuilt the register post type like so:

add_action('init', 'lugar_register');

function lugar_register() {

$labels = array(
    'name' => _x('Lugares', 'post type general name'),
    'singular_name' => _x('Lugar', 'post type singular name'),
    'add_new' => _x('Agregar nuevo', 'lugar'),
    'add_new_item' => __('Agregar lugar'),
    'edit_item' => __('Editar lugar'),
    'new_item' => __('Nuevo lugar'),
    'view_item' => __('Ver lugar'),
    'search_items' => __('Buscar lugares'),
    'not_found' =>  __('No hay lugares'),
    'not_found_in_trash' => __('No hay lugares en la papelera'),
    'parent_item_colon' => ''
);

$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array('title','editor','author','thumbnail','comments','custom-fields')
  );

register_post_type( 'lugar' , $args );

}

You have to be very careful with naming custom types.

share|improve this answer
    
I am glad you got the solution. :) –  booota Sep 22 '11 at 5:24
add comment

Seems like you've found the issue, but for other readers, I've found Gravity Forms to be of great help with this, if you combine it with this wp.org plugin for it to support Custom Post Types.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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