0

I have been working on trying to get data to store and display on a CPT field. The code I have is here:

function fhaac_subject_box_callback( $post ){
     wp_nonce_field( basename(__FILE__ ), 'fhacc_subject_nounce');
     $fhaac_stored_subject_meta = get_post_meta( '$post->ID' );
?>

<div>Subject name: <input type="text" name="fhaac_subject_name" id="fhaac_
subject_name" value="<?php if ( ! empty ( $fhaac_stored_subject_meta 
['fhaac_subject_name']) ) echo esc_attr ( $fhaac_stored_subject_meta 
['fhaac_subject_name'][0] ); ?>"/></div>

I have set the save_post into a function below:

function fhaac_save_subject_meta( $post_id ){
//Checking save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'fhacc_subject_nounce' ] ) && 
wp_verify_nonce( $_POST[ 'fhacc_subject_nounce' ], basename(__FILE__ 
)))?'true' : 'false';

//Exit scripts depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce){
    return;
}
if ( isset( $_POST['fhaac_subject_name'])){
    update_post_meta( $post_id, 'fhaac_subject_name', 
    sanitize_text_field($_POST['fhaac_subject_name']));
}

But either the data isn't saving, or being returned. The issue is, I have this working on a wp_editor field. I'm still fairly new to this, so any help would be really appreciated.

Thanks in advance!

0

Try Like below

function fhaac_subject_box_callback( $post ){
 wp_nonce_field( basename(__FILE__ ), 'fhacc_subject_nounce');
 $fhaac_stored_subject_meta = get_post_meta( '$post->ID' );
 $fhaac_subject_name = get_post_meta($post->ID, 'my_data_subjectName', true);
?>

<div>Subject name: <input type="text" name="fhaac_subject_name" id="fhaac_
subject_name" value="<?= $fhaac_subject_name; ?>"/></div>
  • It kind of worked. It output a value and data, but then when trying to save over, it disappears and doesn't save. This is even after I add the 'save_post' hook. Ideas? – Ollie Jul 12 '17 at 22:53
  • Figured it out! I had "$post_id" instead of just $post_id. Only got there after looking at your code @omar thanks for your help! – Ollie Jul 12 '17 at 22:56
0

Needed to remove quatation marks on "$post_id":

$fhaac_stored_subject_meta = get_post_meta( '$post->ID' );

needed to become

$fhaac_stored_subject_meta = get_post_meta( $post->ID );

All sorted now.

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.