I have created a custom post type and also added some meta boxes. I am able to display the custom post type on the front end as well as built in elements such as post title and featured image. When saving information in my meta box it clears the fields when I save the post. Here is the code:
add_action( 'add_meta_boxes', 'fellowship_church_information' );
function fellowship_church_information() {
add_meta_box(
'fellowship_church_information', // Unique ID
__( 'Church Information', 'myplugin_textdomain' ), // Title
'fellowship_church_information_content', // Callback function
'fellowship', // Admin page (or post type)
'normal', // Context
'default' // Priority
);
}
function fellowship_church_information_content( $post ) {
echo '<div class="fellowship-church-information-form">
<label for="church_name">Church Name: </label> <input type="text" id="church_name" name="church_name" placeholder="" /> <br />
<label for="church_address">Church Address: </label> <input type="text" id="church_address" name="church_address" placeholder="" /> <br />
<label for="church_city">Church City: </label> <input type="text" id="church_city" name="church_city" placeholder="" />
<label for="church_state">Church State: </label>
<select id="church_state" name="church_state" placeholder="">
<option value="AL">Alabama</option>
<option value="WY">Wyoming</option>
</select>
<label for="church_zip">Church Zip: </label> <input type="text" id="church_zip" name="church_zip" placeholder="" />
<input type="hidden" name="fellowship_noncename" id="fellowship_noncename" value="' . wp_create_nonce( 'fellowship-nonce' ) . '" />
</div>
';
}
// SAVING THE INFORMATION TO THE DATABASE
add_action( 'save_post', 'fellowship_church_information_save', 10, 6 );
function fellowship_church_information_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( 'fellowship' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return;
} else {
if ( !current_user_can( 'edit_post', $post_id ) )
return;
}
if (isset($_POST['fellowship_noncename'])){
if ( !wp_verify_nonce( $_POST['fellowship_noncename'], 'fellowship-nonce' ) )
return;
}else{
return;
}
$fellowship_church_information = $_POST['fellowship_church_information'];
update_post_meta( $post_id, 'fellowship_church_information', $_POST['fellowship_church_information'] );
}
// ADD SHORTCODE -- [fellowships]
function fellowships_func( $atts ){
// DISPLAYING THE INFORMATION
$queryFellowship = array(
'post_type' => 'fellowship',
);
$fellowships = new WP_Query( $queryFellowship );
if( $fellowships->have_posts() ) {
while( $fellowships->have_posts() ) {
$fellowships->the_post();
echo '<h1>' . the_title() . '</h1>';
echo '<div class="fellowships-content">';
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
the_content();
get_metadata();
$meta = get_post_meta( get_the_ID() );
echo $meta;
echo '</div>';
}
}
else {
echo '<h2 class="no-fellowships">There are currently no upcoming Alumni & Friends Fellowships.</h2>';
}
}
add_shortcode( 'fellowships', 'fellowships_func' );
Again, I am able to display the title and the featured image but not the custom meta.