get_post_meta not working inside loop

I was hoping for some assistance with a custom meta box that I added to my genesis child theme.

The data is being saved properly, and stored properly, but when I try to echo it out using get_post_meta, nothing shows up. But a custom function someone showed me does echo it out. Can someone figure out what it isn’t working?

Read More

This should work but doesn’t.

add_action ('genesis_before_post_content', 'gteh_tagline');
function gteh_tagline() {
    $meta = get_post_meta($post->ID, $field['dbt_text'], true);
    echo $meta;
    }

This works but I don’t want to use this, would rather use the proper code

add_action ('genesis_before_post_content', 'gteh_tagline');
function gteh_tagline() {
    echo "<pre>";
    $customs = get_post_custom(get_the_ID());
    $text = (isset($customs['dbt_text'][0]))?$customs['dbt_text'][0]:"";
    var_dump($customs);
    echo "<br/>text=".$text;
    echo "</pre>";
}

It dumps this and echoes it out when asked.

["dbt_text"]=>
array(1) {
[0]=>
string(89) "At vero eos et accusamus et iusto odio dignityr simos ducimus qui blanditiis praesentium "
}

Here is dbt_text

$prefix = 'dbt_';
$meta_box = array(
'id' => 'my-meta-box',
'title' => 'Post Tagline',
'page' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
    array(
        'name' => 'Post Tagline',
        'desc' => 'Displayed below the post title',
        'id' => $prefix . 'text',
        'type' => 'text',
        'std' => ''
    )
)

);

Related posts

Leave a Reply

3 comments

  1. There is nothing ‘improper’ about the second block of code. Its just written for debugging purposes. The key difference is that that second block used get_the_ID(). Try that.

    add_action ('genesis_before_post_content', 'gteh_tagline');
    function gteh_tagline() {
        $meta = get_post_meta(get_the_ID(), $field['dbt_text'], true);
        echo $meta;
    }
    

    Or try to pull in $post with global.

    add_action ('genesis_before_post_content', 'gteh_tagline');
    function gteh_tagline() {
        global $post;
        $meta = get_post_meta($post->ID, $field['dbt_text'], true);
        echo $meta;
    }
    

    Or, depending on how that action is written, this might work.

    add_action ('genesis_before_post_content', 'gteh_tagline');
    function gteh_tagline($post) {
        $meta = get_post_meta($post->ID, $field['dbt_text'], true);
        echo $meta;
    }
    
  2. You need to set $post to global within a function:

    function wpse_16722_tagline() {
        global $post;
    
        $meta = get_post_meta( $post->ID, $field['dbt_text'], true );
            echo $meta;
        }
    add_action ('genesis_before_post_content', 'wpse_16722_tagline');
    
  3. Two problems with the first function:

    1. Use get_the_ID() or global the $post before using $post->ID.

    2. $field[‘dbt_text’] isn’t defined in the function anywhere. Is this supposed to be coming from a global as well?

    Edit: After reading your comments and replies to the other answers, I think you don’t understand how PHP’s variable scoping works. You may want to read up on this in order to understand how variables work inside functions:

    http://php.net/manual/en/language.variables.scope.php