简体   繁体   中英

How to add a category to a post in Wordpress via PHP code?

I can't get this to work. I have a page where users can submit their posts. There, ideally I want to show all the categories and let them choose via a dropdown the category to assign the post. I couldn't manage this as I am a newbie regarding PHP so I decided to go step by step and first start with adding an empty input field where users can just add a random text that will become the category of that post.

To do so, I have done the following:

                   if(isset($_POST['entry']) AND !$_POST['entry'] == ""):
                        $my_post = array();
                        $my_post['post_title'] = $_POST['title'];
                        $my_post['post_content'] = $_POST['entry'];
                        $my_post['post_status'] = 'publish';

                        $cat_name = sanitize_text_field($_POST['newcat']);
                        $my_post ['cat_name'] = $cat_name; 

                        $my_post['post_author'] = get_current_user_id();;

                        // add post to database
                        $id = wp_insert_post( $my_post );
                        endif;

So, as you can see, I am trying to add the category by linking it to $my_post and the "newcat" input field.

And the form:

<form action="" method="post" role="form">

<label for="newcat">Category Name</label>
<input type="text" name="newcat" value="" />

</form>

Post title and content are working (removed it from the code above), but category isn't working at all.

Can someone help me and explain what I am doing wrong and how I can solve this?

Edit This is also not working:

  if(isset($_POST['entry']) AND !$_POST['entry'] == ""):
$my_post = array();

$my_post['post_title']   = $_POST['title'];
$my_post['post_content'] = $_POST['entry'];
$my_post['post_status']  = 'publish';

$cat_name             = sanitize_text_field( $_POST['newcat'] );
$my_post ['cat_name'] = $cat_name;

$category_id = get_cat_ID( $_POST['newcat'] );

if ( ! $category_id ) {
    if ( ! is_admin() ) {
        die();
    }
$args = array(
    'description' => "Category description",
    'parent' => 0);
    $category_id = wp_insert_term( $_POST['newcat'], "category", $args );
}

$my_post['post_author'] = get_current_user_id();

$my_post['tax_input'] = array('category' => $category_id);


wp_insert_post($my_post);

Here, the post is not even added to Wordpress if that specific category doesn't exist! No errors at all, but also no post...

Please, consider to use tax_input param for adding the category.

Also, you could create a category if there is not, but be careful with permission restriction.

Please, don`t allow all users to create a category. Check if is an admin for example.

$my_post = array();

$my_post['post_title']   = $_POST['title'];
$my_post['post_content'] = $_POST['entry'];
$my_post['post_status']  = 'publish';

$cat_name             = sanitize_text_field( $_POST['newcat'] );
$my_post ['cat_name'] = $cat_name;

$category_id = get_cat_ID( $_POST['newcat'] );

if ( ! $category_id ) {
    if ( ! is_admin() ) {
        die();
    }

    $args        = [ 'description' => "Category description", 'parent' => 0 ];
    $category_id = wp_insert_term( $_POST['newcat'], "category", $args );
}

$my_post['post_author'] = get_current_user_id();

$my_post['tax_input'] = [ 'category' => $category_id ];

wp_insert_post( $my_post );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM