Reputation:
I currently work for a site where I need to create posts from Front End. This is my code to create a custom post from frontend of wordpress site.
<?php
$postTitleError = '';
if (isset($_POST['submitted']))
{
if(trim($_POST['postTitle']) === '')
{
$postTitleError = 'Please enter a title.';
$hasError = true;
}
$post_information = array(
'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'product',
'post_status' => 'publish'
);
wp_insert_post( $post_information );
}
?>
And this is my HTML Form:
<form action="" id="primaryPostForm" method="POST">
<!-- Title -->
<input class="addTitle" type="text" name="postTitle" id="postTitle" class="required" />
<!-- Description -->
<input class="addDescription required" name="postContent" id="postContent" />
<!-- Submit button -->
<button type="submit"><?php _e('Add Product', 'framework') ?></button>
</form>
I shown all my tags on the page but I don't know how to assign them into the post. Currently i am able to create posts using my code but all i need is to add tags into my post.
Any idea?
Upvotes: 1
Views: 3430
Reputation: 230
Have you tried adding the following to your $post_information = array():
'tags_input' => array('tag,tag1,tag2');
So your code should look like:
$post_information = array(
'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'product',
'post_status' => 'publish',
'tags_input' => array('tag,tag1,tag2')
);
This assumes you have enabled tags in your custom post type.
Reference:
Upvotes: 1