0

newbie here.

I am struggling around for hours. I am developing my first WP Plugin where I can enter products (product title and description). It seems that wp saves my description in the db, because if I open the product in the backend I can see my saved description. But if I'm trying to echo the description I get an empty value.

Below are some code-snippets:

metabox:

function product_custom_meta(){
    add_meta_box('product_meta', __('Product Title', 'product-textdomain' ), 'product_meta_callback', 'url' );
}
add_action('add_meta_boxes', 'product_custom_meta');

function product_meta_callback( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'product_nonce' );
    $product_stored_meta = get_post_meta( $post->ID );
    ?>

    <p>
        <label for="meta-desc" class="product-row-title"><?php _e( 'Description', 'product-textdomain' )?></label>
        <input type="text" name="meta_desc" id="meta_desc" value="<?php if ( isset ( $product_stored_meta['meta-desc'] ) ) echo $product_stored_meta['meta-desc'][0]; ?>" />
    </p>


    <?php
}

function product_meta_save( $post_id ) {

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

    // Exits script depending on save status
    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return;
    }

    // Checks for input and sanitizes/saves if needed
    if( isset( $_POST[ 'meta_desc' ] ) ) {

        update_post_meta( $post_id, 'meta_desc', sanitize_text_field( $_POST[ 'meta_desc' ] ) );

    }
}
add_action( 'save_post', 'product_meta_save');

And thats how I tried to get my value:

    $desc = get_post_meta( $post->ID, 'meta_desc', true );
echo $desc;

But it didn't worked, I hope someone can help me.

Thanks in advance

1 Answer 1

0

After reviewing your code there are a couple of things you need to change depending on where the meta content is being held.

Try this to get the value

$desc = get_post_meta( get_the_ID(), 'meta_desc', TRUE );
echo  $desc;

That Will be give you the appropriate meta.

3
  • I tried it, but it didn't help. still getting an empty value... where's the best place to echo my $desc? Or are there any other methods?
    – Hannes
    Jul 5, 2017 at 13:37
  • @HannesD. You can echo it into the while loop where you are displaying your products
    – user106916
    Jul 5, 2017 at 13:59
  • I've figured it out! I was always in my index-loop and requested a post-id from the real posts and not from the product-posts. So I created a new post type and made a new loop and put your written code in it. Now everything works.
    – Hannes
    Jul 6, 2017 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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