• Resolved aumsrini

    (@aumsrini)


    While i submit a custom post from front end.All data saved in db but relationship like category is not save.But from admin its works well.

    function wds_handle_frontend_new_post_form_submission() {
    	// If no form submission, bail
    	if ( empty( $_POST ) || ! isset( $_POST['submit-cmb'], $_POST['object_id'] ) ) {
    		return false;
    	}
    	// Get CMB2 metabox object
    	$cmb = wds_frontend_cmb2_get();
    	$post_data = array();
    	// Get our shortcode attributes and set them as our initial post_data args
    	if ( isset( $_POST['atts'] ) ) {
    		foreach ( (array) $_POST['atts'] as $key => $value ) {
    			$post_data[ $key ] = sanitize_text_field( $value );
    		}
    		unset( $_POST['atts'] );
    	}
    	// Check security nonce
    	if ( ! isset( $_POST[ $cmb->nonce() ] ) || ! wp_verify_nonce( $_POST[ $cmb->nonce() ], $cmb->nonce() ) ) {
    		return $cmb->prop( 'submission_error', new WP_Error( 'security_fail', __( 'Security check failed.' ) ) );
    	}
    	// Check title submitted
    	if ( empty( $_POST['bee_listing_title'] ) ) {
    		return $cmb->prop( 'submission_error', new WP_Error( 'post_data_missing', __( 'New post requires a title.' ) ) );
    	}
    	// And that the title is not the default title
    	if ( $cmb->get_field( 'bee_listing_title' )->default() == $_POST['bee_listing_title'] ) {
    		return $cmb->prop( 'submission_error', new WP_Error( 'post_data_missing', __( 'Please enter a new title.' ) ) );
    	}
    
    	/**
    	 * Fetch sanitized values
    	 */
    	$sanitized_values = $cmb->get_sanitized_values( $_POST );
    	// Set our post data arguments
    	$post_data['post_title']   = $sanitized_values['bee_listing_title'];
    	//unset( $sanitized_values['bee_listing_title'] );
    
    	$post_data['listingcat']= $sanitized_values['bee_listing_category'];
    	//$post_data['post_content'] = $sanitized_values['bee_listing_description'];
    	//unset( $sanitized_values['bee_listing_description'] );
    	// Create the new post
    	$new_submission_id = wp_insert_post( $post_data, true );
    
    	// If we hit a snag, update the user
    	if ( is_wp_error( $new_submission_id ) ) {
    		return $cmb->prop( 'submission_error', $new_submission_id );
    	}
    	/**
    	 * Other than post_type and post_status, we want
    	 * our uploaded attachment post to have the same post-data
    	 */
    	unset( $post_data['post_type'] );
    	unset( $post_data['post_status'] );
    	// Try to upload the featured image
    	$img_id = wds_frontend_form_photo_upload( $new_submission_id, $post_data );
    	// If our photo upload was successful, set the featured image
    	if ( $img_id && ! is_wp_error( $img_id ) ) {
    		set_post_thumbnail( $new_submission_id, $img_id );
    	}
    	// Loop through remaining (sanitized) data, and save to post-meta
    	foreach ( $sanitized_values as $key => $value ) {
    		if ( is_array( $value ) ) {
    			$value = array_filter( $value );
    			if( ! empty( $value ) ) {
    				update_post_meta( $new_submission_id, $key, $value );
    			}
    		} else {
    			update_post_meta( $new_submission_id, $key, $value );
    		}
    	}
    	/*
    	 * Redirect back to the form page with a query variable with the new post ID.
    	 * This will help double-submissions with browser refreshes
    	 */
    	wp_redirect( esc_url_raw( add_query_arg( 'post_submitted', $new_submission_id ) ) );
    	exit;
    }
    add_action( 'cmb2_after_init', 'wds_handle_frontend_new_post_form_submission' );

    https://www.ads-software.com/plugins/cmb2/

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom taxonomy relationship not save from front end’ is closed to new replies.