Contents
Overview
This Extra Field Type is available in WordPress Creation Kit Hobbyist or Pro. The Custom Post Type Select Field displays a dropdown of all available entries for a specific Custom Post Type. In the options for this field you can choose what specific post type a user can select from.
Creating a Custom Post Type Select Field
To add a Custom Post Type Select Field to a previously created Custom Meta Box, under the Meta Box Fields tab simply insert a Field Title and under Field Type make sure you select Custom Post Type Select.
The Custom Post Type Select Field contains options to customize it:
- Description – Allows you to specify a description for the Custom Post Type Select Field
- Required – Select whether the field is required or not
- CPT – Select what Custom Post Type should be used in the Custom Post Type Select Field
- Default Value – Set a default value for the Custom Post Type Select Field
Page, Post or Custom Post Type Edit Screen
This is how the Custom Post Type Select Field we created above looks like in the Edit Screen:
Template usage
The following examples are for a Custom Meta Box with the “Group Name” argument “my_meta_name“. Make sure to replace this with the “Group Name” you have set up. The Custom Meta Box below is setup to be attached to a post.
Using the WCK Custom Fields API
The Custom Post Type Select Field returns the post title or post object.
For a Single Meta Box
To output a value inside the loop we use the function get_cfc_field() which echoes the post title:
1 | Post Title:<?php the_cfc_field('my_meta_name', 'testimonial'); ?> |
To assign the value to a variable we use the function get_cfc_field() which for the Custom Post Type Select Field returns the post object:
1 2 3 | <?php $post_object = get_cfc_field('my_meta_name', 'testimonial'); ?> |
For a Repeater Meta Box
To output all the “Testimonial” entries from the repeater field we use the functions get_cfc_meta() and get_cfc_field():
1 2 3 4 5 | <?php foreach( get_cfc_meta( 'my_meta_name' ) as $key => $value ){ the_cfc_field( 'my_meta_name','testimonial', false, $key ); } ?> |
To output a specific “Testimonial” entry from the repeater field, for example the second entry, we use the function get_cfc_field():
1 | Post Title: <?php the_cfc_field( 'my_meta_name','testimonial', false, 1 ); ?> |
The index starts at 0 so that’s why we pass “1” to the function. For the first entry it would be “0”, the second is “1”, the third is “2” and so on…
Using the WordPress default functions
The Custom Post Type Select Field returns the post id.
For a Single Meta Box
1 2 3 4 5 6 7 8 | <?php $my_meta = get_post_meta( $post->ID, 'my_meta_name', true ); if( !empty( $my_meta[0]['testimonial'] ) ){ $post_id = $my_meta[0]['testimonial']; $my_post = get_post( $post_id ); echo $my_post->post_title; } ?> |
For a Repeater Meta Box
To output all the “Testimonial” entries in the repeater field:
1 2 3 4 5 6 7 8 9 10 | <?php $my_meta = get_post_meta( $post->ID, 'my_meta_name', true ); if( !empty( $my_meta ) ){ foreach( $my_meta as $entry ){ $post_id = $entry['testimonial']; $my_post = get_post( $post_id ); echo $my_post->post_title; } } ?> |
To output a specific “Testimonial” entry from the repeater field, for example the second entry:
1 2 3 4 5 6 7 8 | <?php $my_meta = get_post_meta( $post->ID, 'my_meta_name', true ); if( !empty( $my_meta[1]['testimonial'] ) ){ $post_id = $my_meta[1]['testimonial']; $my_post = get_post( $post_id ); echo $my_post->post_title; } ?> |
The index starts at 0 so that’s why we pass “1” to the function. For the first entry it would be “0”, the second is “1”, the third is “2” and so on…