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 have a front-end posting form configured for a custom post type. By using the form I am able to send the title, content, and image attachments without a hitch, but my custom taxonomy selection doesn't register. I've patched this functionality using various bits of code I have found and I'm not sure exactly what I screwed up.

add-new-listing.php

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {

    // Do some minor form validation to make sure there is content
    if (isset ($_POST['title'])) {
        $title =  $_POST['title'];
    } else {
        echo 'Please enter the listing name';
    }
    if (isset ($_POST['description'])) {
        $description = $_POST['description'];
    } else {
        echo 'Please enter a description';
    }

    $tags = $_POST['post_tags'];

    // ADD THE FORM INPUT TO $new_post ARRAY
    $new_post = array(
    'post_title'    =>  $title,
    'post_content'  =>  $description,
    'post_category' =>  array($_POST['booru_locations']),  // Usable for custom taxonomies too
    'post_image'    => $newupload,
    'post_status'   =>  'publish',           // Choose: publish, preview, future, draft, etc.
    'post_type' =>  'booru_directory'  //'post',page' or use a custom post type if you want to
    );

    //SAVE THE POST
    $pid = wp_insert_post($new_post);


    //REDIRECT TO THE NEW POST ON SAVE
    $link = get_permalink( $pid );
    wp_redirect( $link );

//INSERT OUR MEDIA ATTACHMENTS
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$pid);
// $newupload returns the attachment id of the file that
// was just uploaded. Do whatever you want with that now.
}

} // END THE IF STATEMENT FOR FILES

} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM

//POST THE POST YO
do_action('wp_insert_post', 'wp_insert_post');

functions.php

$labels = array(
    'name'                          => 'Locations',
    'singular_name'                 => 'Location',
    'search_items'                  => 'Search Locations',
    'popular_items'                 => 'Popular Locations',
    'all_items'                     => 'All Locations',
    'parent_item'                   => 'Parent Location',
    'edit_item'                     => 'Edit Location',
    'update_item'                   => 'Update Location',
    'add_new_item'                  => 'Add New Location',
    'new_item_name'                 => 'New Location',
    'separate_items_with_commas'    => 'Separate Locations with commas',
    'add_or_remove_items'           => 'Add or remove Location',
    'choose_from_most_used'         => 'Choose from most used Locations'
    );

$args = array(
    'label'                         => 'Locations',
    'labels'                        => $labels,
    'public'                        => true,
    'hierarchical'                  => true,
    'show_ui'                       => true,
    'show_in_nav_menus'             => true,
    'args'                          => array( 'orderby' => 'term_order' ),
    'rewrite'                       => array( 'slug' => 'place', 'with_front' => false ),
    'query_var'                     => true
);

register_taxonomy( 'booru_locations', 'booru_directory', $args );


// Add new taxonomy, NOT hierarchical (like tags)
    $labels = array(
        'name' => _x( 'Services', 'Service Type' ),
        'singular_name' => _x( 'Service', 'Service' ),
        'search_items' =>  __( 'Search Services' ),
        'popular_items' => __( 'Popular Services' ),
        'all_items' => __( 'All Services' ),
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Edit Services' ),
        'update_item' => __( 'Update Service' ),
        'add_new_item' => __( 'Add New Service' ),
        'new_item_name' => __( 'New Service' ),
        'separate_items_with_commas' => __( 'Separate Services With Commas' ),
        'add_or_remove_items' => __( 'Add or Remove Services' ),
        'choose_from_most_used' => __( 'Choose From Most Used Services' )
    );

register_taxonomy( 'services', 'booru_directory', array(
        'hierarchical' => false,
        'labels' => $labels, /* NOTICE: the $labels variable here */
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'services' ),
    ));




register_post_type( 'booru_directory',
    array(
        'labels'                => array(
            'name'              => __( 'Directory' ),
            'singular_name'     => __( 'Listing' )
            ),
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'supports'              => array( 'title', 'editor', 'thumbnail' ),
        'rewrite'               => array( 'slug' => 'directory', 'with_front' => false ),
        'has_archive'           => true
    )
);
share|improve this question
    
Is $_POST['booru_locations'] a category id, or is a category name? –  m0r7if3r Mar 7 '12 at 20:15
    
it's a category id –  Richard Mar 7 '12 at 20:26

1 Answer 1

Take a look at the Codex back for wp_insert_post. I've not checked, but I do not think this is correct:

 'post_category' =>  array($_POST['booru_locations']),  // Usable for custom taxonomies too

The array passed to wp_insert_post, however can contain a tax_input key, whose value is an array of arrays:

 'tax_input' =>array( 'booru_locations' => array( 'term', 'term2', 'term3' ) )

The value for 'booru_locations' must an array of strings (for term slugs) or integers (for term IDs). If you are using term IDs you must make sure they are integers by casting them as such.


So $_POST['booru_locations'] contains a term ID for the taxonomy 'booru_locations'. First, make sure its an integer (not a string representation of) and create an array of IDs (in this case there will be only one):

 $location_id = intval($_POST['booru_locations']);
 $locations = (!empty($location_id) ? array($location_id) : array());

So $locations an array of integers (term IDs). We can then add this to the $new_post array

// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
'post_title'    =>  $title,
'post_content'  =>  $description,
'post_image'    => $newupload,
'post_status'   =>  'publish',  
'post_type' =>  'booru_directory',
'tax_input' =>  array('booru_locations'=>$locations),
);
share|improve this answer
    
I don't understand. Could you elaborate? –  Richard Mar 7 '12 at 20:34
    
Certainly, see updated answer with example –  Stephen Harris Mar 7 '12 at 20:58
    
Where am I supposed to put $location_id = intval($_POST['booru_locations']); $locations = (!empty($location_id) : array($location_id) ? array()); –  Richard Mar 7 '12 at 23:14
    
Same place you put $tags = $_POST['post_tags']; –  Stephen Harris Mar 7 '12 at 23:17
    
I thought so, but I tried that, I got this message Parse error: syntax error, unexpected ':' in /home/cbooru/public_html/wp-content/themes/camerabooru_theme/add-new-listing.php on line 23 –  Richard Mar 7 '12 at 23:35

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.