Skip to main content

Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #264378
    webinuse
    Participant

    I have set up everything for adding posts from frontend, choosing categories, adding images, files… everything. But I can not seem to find solution for my problem. User can post from form that automatically must have category called eg. “small service.” I have several “main” categories eg. “car”, “motor” and they all have this very subcategory. So when user chooses eg. “car” from dropdown, WordPress must automatically asign this very post to category “car” and its subcategory “small service”. Any help? Please. Thank you in advance.

    $postTitleError = '';
    
    if(isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && 
     wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {
    
    if(trim($_POST['postTitle']) === '') {
        $postTitleError = 'Molimo Vas da unesete naslov.';
        $hasError = true;
    } else {
        $postTitle = trim($_POST['postTitle']);
    }
    $cat_ID = get_cat_ID( $_POST['newcat'] );
    
    //If not create new category
    if($cat_ID == 0) {
       $cat_name = array('cat_name' => $_POST['newcat']);
       wp_insert_category($cat_name);
     }
    
      //Get ID of newly created category
     $new_cat_ID = get_cat_ID($_POST['newcat']);
    
    $post_information = array(
        'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
        'post_content' => esc_attr(strip_tags($_POST['postContent'])),
        'post-type' => 'post',
        'post_status' => 'publish',
        'post_category' => array($new_cat_ID)
    
    );
    
    $post_id = wp_insert_post($post_information);
    
    if($post_id)
    {
    
        // Update Custom Meta
    update_post_meta($post_id, 'vsip_custom_date', esc_attr(strip_tags($_POST['customMetaDate'])));
        update_post_meta($post_id, 'vsip_custom_brojilo', esc_attr(strip_tags($_POST['customMetaBrojilo'])));
        update_post_meta($post_id, 'vsip_custom_ulje', esc_attr(strip_tags($_POST['customMetaUlje'])));
            update_post_meta($post_id, 'vsip_custom_filter', esc_attr(strip_tags($_POST['customMetaFilter'])));
            update_post_meta($post_id, 'vsip_custom_permant', esc_attr(strip_tags($_POST['customMetaPermant'])));
            update_post_meta($post_id, 'vsip_custom_zamjena', esc_attr(strip_tags($_POST['customMetaZamjena'])));
    update_post_meta($post_id, 'vsip_custom_file', esc_attr(strip_tags($_POST['customMetaFile'])));
    
        // Redirect
        wp_redirect( home_url() ); exit;
     }
    
     } 
      ?>
     <?php
     if( ! empty( $_FILES ) ) {
     foreach( $_FILES as $file ) {
       if( is_array( $file ) ) {
         $attachment_id = upload_user_file( $file );
       }
     }
     }
      ?>
        <?php get_header(); ?>
    <!-- #primary BEGIN -->
     <div id="primary">
      <form action="" id="primaryPostForm" method="POST" enctype="multipart/form-data">
      <fieldset>
         <select id="cat" class="cat" name="newcat"  >
            <option value="0"><?php echo esc_attr(__('Izaberite vozilo')); ?></option>
            <?php
               $categories= get_categories(array('orderby ' => 'id' , 'order' => 'DESC' ,'hide_empty' => 0 , 'hierarchical' => true)); 
               foreach ($categories as $category) {
               $option = '<option value="'.$category->cat_name.', mali servis" broj="'.$category->category_count.'" val="'.$category->cat_name.'">';
               $option .= $category->cat_name; 
               $option .= '</option>';
               echo $option;
               }
               ?>
         </select>
      </fieldset>
    
    #264445
    webinuse
    Participant

    Why am I the one that always asks questions with no answers?!

    #264468
    Shikkediel
    Participant

    There are only few PHP/WP specialists around…

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Back End’ is closed to new topics and replies.