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.

this code works fine. the problem i have is that when i submit it only checks the parent category not the subcategory even though i specified both a parent and a subcategory.

please. what am i doing wrong. ohh if you look in the html chunk, the 7 is parent cat and the numbers after the comma are the subcategories.

   <?php 
if(isset($_POST['new_post']) == '1') {
    $post_title = $_POST['post_title'];
    $post_category = $_POST['cat'];
    $post_content = $_POST['post_content'];
    $new_post = array(
          'ID' => '',
          'post_author' => $user->ID, 
          'post_content' => $post_content,
          'post_title' => $post_title,
          'post_status' => 'publish',
          'post_category' => array($post_category)

        );

    $post_id = wp_insert_post($new_post);

    // This will redirect you to the newly created post
    $post = get_post($post_id);
    wp_redirect($post->guid);
}      
?>

this is the html

<form method="post" action="" name="" onsubmit="return checkformf(this);">


<input type="text" name="post_title" size="45" id="input-title"/>

<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea></br>


<ul><select name='cat' id='cat' class='postform' > 
    <option class="level-0" value="7,95">child cat1</option> 
    <option class="level-0" value="7,100">child cat2</option> 
    <option class="level-0" value="7,101">child cat3</option> 
    <option class="level-0" value="7,94">child cat4</option> 
</select> 
</ul>

<input type="hidden" name="new_post" value="1"/> 

<input class="subput" type="submit" name="submitpost" value="Post"/> 


</form>

please let me know if you need more information. thanks in advance

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

The problem is, you can't make an array like that in PHP. Trying to cast a string that contains a comma separated list as an array just produces an array with a single value--your comma separated string.

You want to use php's explode function to create the array. It takes a string an splits it into a bona fide array based on an arbitrary delimiter (in your case, we'll use a comma).

Try something like this:

if(isset($_POST['new_post']) == '1') {
    $post_title = $_POST['post_title'];
    $arr_post_category = explode(',',$_POST['cat']); // EXPLODE!
    $post_content = $_POST['post_content'];
    $new_post = array(
          'ID' => '',
          'post_author' => $user->ID, 
          'post_content' => $post_content,
          'post_title' => $post_title,
          'post_status' => 'publish',
          'post_category' => $arr_post_category // NOW IT'S ALREADY AN ARRAY

        );

    $post_id = wp_insert_post($new_post);

    // This will redirect you to the newly created post
    $post = get_post($post_id);
    wp_redirect($post->guid);
}
share|improve this answer
    
You are awesome. –  andrewk Dec 15 '10 at 3:38
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.