2 years ago
#69526
Marko
Custom front-end form for adding post - Category problem
I've created a CPT
front-end form for post-adding. It has a title, description, categories, image, and a couple of custom fields connected with ACF
in the backend.
Everything is working, except categories. Once the form is submitted, categories are not added in the single backend post.
Here is the code part where the categories are looped on the front-end:
$args = array(
'taxonomy' => 'types',
'hide_empty' => false,
'hierarchical' => 1
);
wp_dropdown_categories( $args );
And here is the process:
if ( isset( $_POST['post_form'] ) ) {
global $current_user;
wp_get_current_user();
$user_login = $current_user->user_login;
$user_id = $current_user->ID;
$author_name = get_the_author_meta( 'ID', $current_user->ID );
$post_title = sanitize_text_field( $_POST['pas_title'] );
$post_category = (int) $_POST['cat'];
$post_content = sanitize_text_field( $_POST['pas_content'] );
$post_phone = sanitize_text_field( $_POST['pas_phone'] );
$post_location = sanitize_text_field( $_POST['pas_location'] );
$post_price = sanitize_text_field( $_POST['pas_price'] );
$post_condition = sanitize_text_field( $_POST['pas_condition'] );
$new_post = array(
'post_title' => $post_title,
'post_content' => $post_content,
'post_status' => 'publish',
'post_name' => str_replace( ' ', '-', $post_title ),
'post_type' => 'ads',
'post_author' => $author_name,
);
$post_id = wp_insert_post( $new_post );
wp_set_object_terms( $post_id, $post_category, 'types' );
add_post_meta( $post_id, 'meta_key', true );
add_post_meta( $post_id, 'cena', $post_price );
add_post_meta( $post_id, 'lokacija', $post_location );
add_post_meta( $post_id, 'kontakt_telefon', $post_phone );
add_post_meta( $post_id, 'stanje_robe', $post_condition );
add_post_meta( $post_id, 'pas_user_id', $user_id );
if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
require_once( ABSPATH . "wp-admin" . '/includes/file.php' );
require_once( ABSPATH . "wp-admin" . '/includes/media.php' );
}
$file_logo = media_handle_upload( 'pas_image', $post_id );
update_post_meta( $post_id, '_thumbnail_id', $file_logo );
}
I've done it before in the same way where everything worked.
If needed, here is the CPT
part:
class PAS_Cpt {
public function __construct() {
add_action( 'init', array( $this, 'pas_cpt_ads' ), 0 );
add_action( 'init', array( $this, 'pas_cpt_ads_taxonomy' ), 0 );
}
/**
* CPT Oglasi
*
* @return void
*/
public function pas_cpt_ads() {
$labels = array(
'name' => __( 'Oglasi' ),
'singular_name' => __( 'Oglas' ),
'menu_name' => __( 'Oglasi' ),
'all_items' => __( 'Svi oglasi' ),
'view_item' => __( 'Pogledaj oglas' ),
'add_new_item' => __( 'Dodaj novi Oglas' ),
'add_new' => __( 'Dodaj novi' ),
'edit_item' => __( 'Edituj oglas' ),
'update_item' => __( 'Ažuriraj oglas' ),
'search_items' => __( 'Pretraži oglas' ),
'not_found' => __( 'Nije pronađen' ),
'not_found_in_trash' => __( 'Nije pronađen u kanti' )
);
$args = array(
'label' => __( 'ads' ),
'labels' => $labels,
'supports' => array(
'title',
'editor',
'excerpt',
'author',
'thumbnail',
'revisions',
'custom-fields'
),
'public' => true,
'hierarchical' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'has_archive' => true,
'can_export' => true,
'exclude_from_search' => false,
'yarpp_support' => true,
'taxonomies' => array(
'post_tag'
),
'publicly_queryable' => true,
'capability_type' => 'post',
'menu_icon' => __( 'dashicons-megaphone' )
);
register_post_type( 'ads', $args );
}
/**
* CPT Oglasi Taxonomy
*
* @return void
*/
public function pas_cpt_ads_taxonomy() {
$labels = array(
'name' => _x( 'Kategorije', 'ads' ),
'singular_name' => _x( 'Kategorija', 'ads' ),
'search_items' => __( 'Pretraži kategorije' ),
'all_items' => __( 'Sve kategorije' ),
'parent_item' => __( 'Parent kategorija' ),
'parent_item_colon' => __( 'Parent kategorija:' ),
'edit_item' => __( 'Edituj kategoriju' ),
'update_item' => __( 'Ažuriraj kategoriju' ),
'add_new_item' => __( 'Dodaj novu kategoriju' ),
'menu_name' => __( 'Kategorije' ),
);
register_taxonomy( 'types', array( 'ads' ), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'type'
),
) );
}
}
new PAS_Cpt();
I've been struggling with this for two days. Any help is appreciated.
Thanks
php
wordpress
advanced-custom-fields
custom-post-type
custom-taxonomy
0 Answers
Your Answer