0

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 postenter image description here

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.

7
  • I think $post is undefined. try replacing $post->ID with get_the_ID() and see if it works. Apr 6 '18 at 16:58
  • I am calling all other Custom Fields from other CPT the same way and they are working without any issues
    – mad2kx
    Apr 6 '18 at 17:03
  • 1
    The add_post_meta in your template should be get_post_meta
    – Milo
    Apr 6 '18 at 17:10
  • Ha @Milo - yep that'd do it. Apr 6 '18 at 17:11
  • @Milo it does not work
    – mad2kx
    Apr 6 '18 at 17:34
1

The post meta form is limited to displaying 30 keys by default. To change the value, use the postmeta_form_limit filter:

add_filter( 'postmeta_form_limit', function( $limit ) {
    return 50;
} );

Also consider adding a custom meta box, where you can set up dedicated fields instead of having to select a key each time.

2
  • Must I add this code into the functions.php?
    – mad2kx
    Apr 6 '18 at 19:29
  • yes, or your own plugin
    – Milo
    Apr 6 '18 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.