Forums

[resolved] Creating post with PHP: Categories (3 posts)

  1. NotAlame
    Member
    Posted 2 years ago #

    Hi!

    I'm making a form on a wp page from where I'll post posts.
    My problem is that I don't know how to get the categories from a <select name"col1"><option> Formular.

    Here is my code:

    if(isset($_POST['Col1']) && !empty($_POST['Col1'])){
     $Col1_Array = $_POST['Col1'];
     $a='';
     foreach($Col1_Array as $selectValue){
      $a= $a.$selectValue.",";
     }
    }
    // Create post object
    $my_post = array();
    $my_post['post_title']    = stripslashes($_POST['titre']);
    $my_post['post_content']  = stripslashes($_POST['description']);
    $my_post['post_status']   = get_user_post_status ();
    $my_post['post_author']   = $current_user->ID;
    $my_post['post_category'] = array($a.'1');
    // Insert the post into the database
    wp_insert_post( $my_post );

    this code only post on the first category that is selected.

    What can I change to make it right?

    Thanks

  2. NotAlame
    Member
    Posted 2 years ago #

    up?

  3. NotAlame
    Member
    Posted 2 years ago #

    here is the solution that I found:

    // Create post object
    $my_post = array();
    $my_post['post_title']    = stripslashes($_POST['titre']);
    $my_post['post_content']  = stripslashes($_POST['description']);
    $my_post['post_status']   = get_user_post_status ();
    $my_post['post_author']   = $current_user->ID;
    $my_post['post_category'] = $_POST['Col1'];
    // Insert the post into the database
    wp_insert_post( $my_post );

    I hope it will help!

Topic Closed

This topic has been closed to new replies.

About this Topic