フロントエンドからカスタム投稿タイプ「質問」を公開したいが、フォームを送信すると404エラーが表示され続ける。ベローはフォームとフォームの処理です。私は間違って何をしていますか?フロントからカスタム投稿タイプを公開する
<?
/**
* Questions processing
*/
if('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['action'])) {
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please add a question';
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please add a description';
}
// Add the content of the form to $post as an array
$post = array(
'post_title' => $title,
'post_content' => $description,
'post_status' => 'publish',
'post_type' => 'question'
);
wp_insert_post($post); // Pass the value of $post to WordPress the insert function
} // end IF
// Do the wp_insert_post action to insert it
do_action('wp_insert_post', 'wp_insert_post');
?>
<h1>Add a question:</h1>
<!-- New Question Form -->
<div>
<form name="new_post" method="post" action="">
<p><label for="title">Question:</label><br />
<input type="text" value="" name="title" />
</p>
<p><label for="description">Details</label><br />
<textarea name="description" cols="50" rows="6"></textarea>
</p>
<p><input type="submit" value="Ask!" name="submit" /></p>
<input type="hidden" name="post_type" value="question" />
<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field('new-post'); ?>
</form>
</div>
<!--// New Question Form -->
'do_action()'呼び出しは 'wp_insert_post($ post)'の直後に置いてください。 –
あなたの検証ステップでは、スクリプトが新しい投稿を保存するのを止めません。それはあなたが欲しいものですか? $ _POSTにタイトルや説明がない場合は、2つの未定義の変数($ titleと$ description)があり、新しい投稿を保存しようとします。 – mishu
wp_insert_post($ post)を置き換えるとどうなりますか? $ return = wp_insert_post($ post); var_dump($ return); – mishu