before I start, I am not using any plugins for my CPT and Custom Fields. I do not want to make use of such plugins.
I am using several Custom Post Types and also include Custom Fields within the posts created. For some odd reason the Custom Fields created under Videos will not be saved. Everytime I have to create a new custom field with the same name and have to add the content again. I cannot pick the respective custom fields in that dropdown list.
The Custom Field and it's content are stored in the Wordpress Data base but Wordpress "forgets" them when I create a new Custom Post.
I created the CPT as follows:
// Video Custom Post Type
function video_init() {
// set up product labels
$labels = array(
'name' => 'Videos',
'singular_name' => 'Video',
'add_new' => 'Add New Video',
'add_new_item' => 'Add New Video',
'edit_item' => 'Edit Video',
'new_item' => 'New Video',
'all_items' => 'All Videos',
'view_item' => 'View Video',
'search_items' => 'Search Videos',
'not_found' => 'No Videos Found',
'not_found_in_trash' => 'No Videos found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Video',
);
// register post type
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array('slug' => 'video'),
'query_var' => true,
'menu_icon' => 'dashicons-video-alt3',
'supports' => array(
'title',
'editor',
'custom-fields',
)
);
register_post_type( 'video', $args );
// register taxonomy
register_taxonomy('video_category', 'video', array('hierarchical' => true, 'label' => 'Category', 'query_var' => true, 'rewrite' => array( 'slug' => 'video-category' )));
}
add_action( 'init', 'video_init' );
?>
Then I am including youtubelink and subtitle into my post
At the end I am calling the CPT including it's post meta as follows
<!--VIDEOS-->
<div id="videos" class="block relative" style="z-index: 90;padding-top: 100px;padding-bottom: 100px; margin-top: 50% !important;">
<h3 class="block">v/deos > </h3>
<?php $query = new WP_Query( array( 'post_type' => 'video' ) ); ?>
<?php
$i = 1;
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php if(($i % 2) == 0) { ?>
<div class="full-width text-left big-big-block video-cnt">
<div class="row">
<div class="col-sm-8" style="width: 100%; height: 100% auto">
<?php if (!((get_post_meta($post->ID, 'youtubelink', TRUE))=='')) {
echo wp_oembed_get( get_post_meta($post->ID, "youtubelink", true) );
}?>
</div>
</div>
<div class="row">
<div class="col-sm-5 col-sm-offset-7 text-left video-description-text">
<h3>
<?php the_title(); ?><br/>
<small><?php echo get_post_meta($post->ID, 'subtitle', true); ?> //////////////////</small></h3>
<div class="block">
<p>
<?php the_content(); ?>
</p>
</div>
</div>
</div>
</div>
<?php } elseif (($i % 2) !== 0) { ?>
<div class="full-width text-left big-big-block video-cnt">
<div class="row">
<div class="col-sm-8 col-sm-offset-4">
<?php if (!((add_post_meta($post->ID, 'youtubelink', TRUE))=='')) {
echo wp_oembed_get( get_post_meta($post->ID, "youtubelink", true) );
}?>
</div>
</div>
<div class="row">
<div class="col-sm-5 text-right video-description-text">
<h3>
<?php the_title(); ?><br/>
<small><?php echo add_post_meta($post->ID, 'subtitle', true); ?> //////////////////</small></h3>
<div class="block">
<p>
<?php the_content(); ?>
</p>
</div>
</div>
</div>
<?php } ?>
<?php $i++; ?>
<?php endwhile; ?>
<?php endif; ?>
<!-- show 404 error here -->
</div>
</div>
So now, the moment I create a new Video CPT I have to create the youtubelink and subtitle again. This is frustrating me as everything works perfect its just that under this specific CPT I cannot figure out why I have to create the metafields over and over again.
add_post_meta
in your template should beget_post_meta