I'm creating a buddypress site in which a single post will create the trunk of a related series of articles. From that trunk article, related branches of posts will stem outwards.
Anyone signed up on the site can create an initial trunk article. Anyone can also respond to the trunk article.
What I need to do is keep all hierarchical posts that stem from the trunk article in the same category.
So when a user creates the trunk post, in the front-end post form they add a post title, and content, and at the same time I'd like the category automatically created. The title of the category would be the same as the post title.
Then for any hierarchal posts stemming from this trunk article, the category will already be applied to them as well.
Here is my front-end post form:
<?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 Title';
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please enter some text';
}
$tags = $_POST['post_tags'];
// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
'post_title' => $title,
'post_content' => $description,
'post_category' => array(5),//array($_POST['cat']), Usable for custom taxonomies too
'tags_input' => array($tags),
'post_status' => 'publish', // Choose: publish, preview, future, draft, etc.
'post_type' => 'post', //'post',page' or use a custom post type if you want to
);
//SAVE THE POST
$pid = wp_insert_post($new_post);
//KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
wp_set_post_tags($pid, $_POST['post_tags']);
//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');
?>
<?php get_header(); ?>
<div id="container">
<div id="content" role="main">
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( is_front_page() ) { ?>
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php } else { ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php } ?>
<div class="form-content">
<?php the_content(); ?>
<!-- CREATE TRUNK FORM -->
<div>
<form id="new_post" name="new_post" method="post" action="" class="form-stacked storyForm" enctype="multipart/form-data">
<!-- post name -->
<legend>* = indicates a required field</legend>
<fieldset name="name">
<label for="title">* Story Title:</label>
<input type="text" style="width:100%" id="title" value="" tabindex="5" name="title" rel="popover" data-content="Name your story." data-original-title="Story Title"/>
</fieldset>
<script>
$(function () {
$("#title[rel=popover]")
.popover({
offset: 10
})
.click(function(e) {
e.preventDefault()
})
})
</script>
<!-- post tags -->
<fieldset class="tags">
<label for="post_tags">Additional Keywords (comma separated):</label>
<input type="text" style="width:100%" value="" tabindex="35" name="post_tags" id="post_tags" rel="popover" data-content="This will just help others find your story in searches." data-original-title="Additional Keywords" />
</fieldset>
<script>
$(function () {
$("#post_tags[rel=popover]")
.popover({
offset: 10
})
.click(function(e) {
e.preventDefault()
})
})
</script>
<!-- post Content -->
<fieldset class="content">
<label for="description">Write your story here:</label>
<textarea id="description" tabindex="15" name="description" cols="80" rows="10" rel="popover" data-content="Write as much as you want. Let others finish it." data-original-title="Main Content"></textarea>
</fieldset>
<script>
$(function () {
$("#description[rel=popover]")
.popover({
offset: 10
})
.click(function(e) {
e.preventDefault()
})
})
</script>
<br>
<fieldset class="submit">
<input class="btn btn-primary pull-right" type="submit" value="Submit" tabindex="40" id="submit" name="submit" />
</fieldset>
<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>
Right now I have "'post_category' => array(5)
" but it needs to dynamically create a new post category.