2
votes

Please help me.. im new to wordpress, im creating a custom post with the custom_meta_box of the field (Location, Dress)

So on my listing of custom post i want to view the value that i created on the custom_meta_box.

here is my current code:


    /*
    Plugin Name: My Own Custom Post
    Plugin URI: http://www.mywebsite.com/firstPlugin/
    Description: My Sample Description
    Author: Monski
    Version: 1.0
    Author URI: http://www.mywebsite.com/
    */


    // Registers the new post type and taxonomy
    function wpt_event_posttype() {
        register_post_type( 'events',
            array(
                'labels' => array(
                    'name' => __( 'Events' ),
                    'singular_name' => __( 'Event' ),
                    'add_new' => __( 'Add New Event' ),
                    'add_new_item' => __( 'Add New Event' ),
                    'edit_item' => __( 'Edit Event' ),
                    'new_item' => __( 'Add New Event' ),
                    'view_item' => __( 'View Event' ),
                    'search_items' => __( 'Search Event' ),
                    'not_found' => __( 'No events found' ),
                    'not_found_in_trash' => __( 'No events found in trash' )
                ),
                'public' => true,
                'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ),
                'capability_type' => 'post',
                'rewrite' => array("slug" => "events"), // Permalinks format
                'menu_position' => 5,
                'register_meta_box_cb' => 'add_events_metaboxes'
            )
        );
    }
    add_action( 'init', 'wpt_event_posttype' );

    function add_events_metaboxes() {
       // add_meta_box('wpt_events_date', 'Event Date', 'wpt_events_date', 'events', 'side', 'default');
        add_meta_box('wpt_events_location', 'Event Location', 'wpt_events_location', 'events', 'normal', 'high');
    }

    add_action( 'add_meta_boxes', 'add_events_metaboxes' );

    function wpt_events_location() {
        global $post;
        // Noncename needed to verify where the data originated
        echo '';
        // Get the location data if its already been entered
            $location = get_post_meta($post->ID, '_location', true);
            $dresscode = get_post_meta($post->ID, '_dresscode', true);
        // Echo out the field
            echo '

Enter the location:

'; echo ''; echo '

How Should People Dress?

'; echo ''; } function wpt_save_events_meta($post_id, $post) { // verify this came from the our screen and with proper authorization, // because save_post can be triggered at other times if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) { return $post->ID; } // Is the user allowed to edit the post or page? if ( !current_user_can( 'edit_post', $post->ID )) return $post->ID; // OK, we're authenticated: we need to find and save the data // We'll put it into an array to make it easier to loop though. $events_meta['_location'] = $_POST['_location']; $events_meta['_dresscode'] = $_POST['_dresscode']; // Add values of $events_meta as custom fields foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array! if( $post->post_type == 'revision' ) return; // Don't store custom data twice $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely) if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value update_post_meta($post->ID, $key, $value); } else { // If the custom field doesn't have a value add_post_meta($post->ID, $key, $value); } if(!$value) delete_post_meta($post->ID, $key); // Delete if blank } } add_action('save_post', 'wpt_save_events_meta', 1, 2); // save the custom fields
3

3 Answers

1
votes

You want to use the get_post_meta function. This will provide the value and allow you to display it anyway in your page/post templates.

Hopefully this article will help as well looking at the whole process.

1
votes

Update your code with that code and it worked for me. I prefer to use ACF(Advanced Custom Fields), but it is good to learn how to metabox. With heavy code its better for your knowledge

add_action('init','wpt_event_posttype');
add_action('save_post','save_event_data');


function add_events_metaboxes(){
    add_meta_box('events',esc_html__('Info'), 'events_callback', 'events', 'normal', 'default', null);
}


//Events Callback function Meta_Box
function events_callback($post){
    wp_nonce_field('save_event_data','event_nonce');

    $location = get_post_meta($post->ID,'_event_location_key',true);
    echo '<label for="event_location_field" style="display:block">Location</label></br>';
    echo '<input type="text" id="event_location_field" name="event_location_field" style="width:400px" value="'. esc_attr__($location).'"/></br>';

    $dresscode = get_post_meta($post->ID,'_event_dresscode_key',true);
    echo '<label for="event_dresscode_field" style="display:block">Dresscode</label></br>';
    echo '<input type="text" id="event_dresscode_field" name="event_dresscode_field" style="width:400px" value=" '. esc_attr__($dresscode). '" /></br>';

}
function save_event_data($post_id){
    if (!isset($_POST['event_nonce'])
        || ! wp_verify_nonce($_POST['event_nonce'],'save_event_data')
        || ! isset($_POST['event_location_field'])
        || ! isset($_POST['event_dresscode_field'])){
        return;
    }
    $event_location = sanitize_text_field($_POST['event_location_field']);
    update_post_meta($post_id,'_event_location_key',$event_location);

    $event_dresscode = sanitize_text_field($_POST['event_dresscode_field']);
    update_post_meta($post_id,'_event_dresscode_key',$event_dresscode);
}

and for your html use this code

<?php 
     $location = get_post_meta(get_the_ID(),'_event_location_key') ; 
     $dresscode = get_post_meta(get_the_ID(),'_event_dresscode_key');
?>

echo $location[0];
echo $dresscode[0];

0
votes

I would suggest you to use the below plugin. Using ACF you can create custom fields for your custom post type.

https://wordpress.org/plugins/advanced-custom-fields/