How exactly is it failing at the moment?
A quick lookover says it may not have access to the $post variable yet, and you may need to do global $post first.
Thread Starter
andp
(@andp)
Hi let me be more specific. As i mentioned based on this snippet (https://github.com/WebDevStudios/CMB2-Snippet-Library/blob/master/front-end/cmb2-front-end-submit.php) i created a frontend submit form for a custom
post type. furthermore i used ‘post_author’ => $user_id.
In order to submit (using the above form) the user must be logged-in.
Then i am displaying this information on the frontend using a function which looks like this
function details(){
$postid = get_the_ID();
$object_id = $postid;
$metabox_id = ‘test_metabox’;
cmb2_metabox_form( $metabox_id, $object_id );
}
add_shortcode( ‘details’, ‘details’ );
what i would like to do is to make this information be editable only by the author (frontend). if somebody else is accessing the post he should just see the cmb2 meta vales and not being able to edit them.
Thread Starter
andp
(@andp)
done the first part
function details(){
global $post,$current_user;
get_currentuserinfo();
if ($post->post_author == $current_user->ID) {
$object_id = $postid;
$metabox_id = ‘test_metabox’;
cmb2_metabox_form( $metabox_id, $object_id );
}
return false;
what i want now is to get cmb2 values outside the form, so that they wont be editable. any quick hint?
Thread Starter
andp
(@andp)
On my front end submit form i am using something like this
$cmb->add_field( array(
‘name’ => __( ‘Town’, ‘wds-post-submit’ ),
‘id’ => ‘submitted_town’,
‘type’ => ‘text’,
) );
What i would like to do is to display the value of this entry. Just the value. I tried get_post_meta( $post_id, ‘submitted_town’, true ); but still cant get the value.
any quick hint?
You should be able to get values from the form using either get_post( $post_id ) or get_post_meta( $post_id, 'ENTER KEY HERE', true ), so standard WP functions at that point.
Thread Starter
andp
(@andp)
yeap u r right i managed to do it by
<?php echo get_post_meta(get_the_ID(), ‘submitted_town’, true); ?>
thank you
Thread Starter
andp
(@andp)
my metaboxes include too many fields. by this <?php echo get_post_meta(get_the_ID(), ‘submitted_town’, true); ?> i can get the value filed as u suggested.
i was wondering though if there is a way to display the entire metabox without being editable, just the values.
thank u
Each part of the data being save in your fields are going to have their own meta keys. 5 by the looks of the originally linked example, more if you’ve expanded on it.
It would be possible to fetch ALL meta on the post and just loop over those and display everything, but you may not want absolutely everything that WP saves as meta. So you’d need to filter out the parts you don’t want to display before you do the display.