0

I'm using this front end post submission form that I took from this question:

<?php 

$postTitle = $_POST['post_title'];
$post = $_POST['post'];
$submit = $_POST['submit'];
$category = $_POST['cat'];

if(isset($submit)){

    global $user_ID;

    $new_post = array(
        'post_title' => $postTitle,
        'post_content' => $post,
        'post_status' => 'publish',
        'post_date' => date('Y-m-d H:i:s'),
        'post_author' => $user_ID,
        'post_type' => 'post',
        'post_category' => $category
    );

    wp_insert_post($new_post);

}

?>
<!DOCTYPE HTML SYSTEM>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled Document</title>
</head>

<body>
<div id="wrap">
<form action="" method="post">
<table border="1" width="200">
  <tr>
    <td><label for="post_title">Post Title</label></td>
    <td><input name="post_title" type="text" /></td>
  </tr>
  <tr>
    <td><label for="post">Post</label></td>
    <td><input name="post" type="text" /></td>
  </tr>
  <tr>
    <td><label for="post_category">Category</label></td>
    <td><?php wp_dropdown_categories('show_count=1&hierarchical=1&hide_empty=0'); ?></td>
  </tr>
</table>

<input name="submit" type="submit" value="submit" />
</form>
</div>

</body>
</html>

Now, I'm trying to add the category field:

Top of the file:

$category = $_POST['cat'];

The $post array:

 'post_category' => $category

Form part:

<?php wp_dropdown_categories('show_count=1&hierarchical=1&hide_empty=0'); ?>

The HTML for the drop down menu displays but when I select another category say, Jobs, the post is published as Uncategorized.

Any suggestions to fix this?

0

From the Codex:

Categories need to be passed as an array of integers that match the category IDs in the database. This is the case even where only one category is assigned to the post.

Currently you giving it a string of the category ID.

Try:

$category = array(intval($_POST['cat']));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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