0

I'm making a custom meta field for posts.

In the back-end, get_post_meta works fine and returns the value.

In the front-end, it returns NULL:

$my_custom_field = get_post_meta( $post_id, 'my_custom_field', true );

var_dump($my_custom_field); // NULL
echo $my_custom_field // ''

Things I tried and looked into:

  1. my_custom_field gets written to the database with corresponding post_id, and everything seems fine.

  2. Using hard-coded post ID gives the same result

  3. Putting the call inside the loop gives the same result

QUESTION: Why get_post_meta returns NULL, how to fetch the actual value?

EDIT: The field is right there in the database:

meta_id     post_id     meta_key                        meta_value

139 87 my_custom_field IT works!

The function arguments reference correct post_id and meta_key which I have checked multiple times, including hard-coding the arguments.

I have also tried to change the name of the meta_key as suggested in some other answer - didn't work.

Backend code is basically this tutorial https://www.smashingmagazine.com/2011/10/create-custom-post-meta-boxes-wordpress/

EDIT 2:

No errors in my logs.

Here is the complete front-end code which returns NULL instead of expected value:

add_action( 'the_post', 'output_my_custom_field');
function output_my_custom_field() {
    $post_id = get_the_ID();

    if ( !empty( $post_id ) ) {
        $my_custom_field = get_post_meta( $post_id, 'my_custom_field', true );

        var_dump($my_custom_field);
    }   
}

EDIT 3: Regardless of the hook used (I tried various ones), or even calling a function without any hooks - it still returns NULL.

17
  • 1
    Did you a make a var_dump of $post_id too? On the frontend i mean? :) Nov 27, 2016 at 8:35
  • Yes, it returns the correct post ID. Nov 27, 2016 at 8:37
  • 2
    Post your backend code. Nov 27, 2016 at 10:19
  • if you pass no key as a parameter: get_post_meta( $post_id, '', true ) it should return you an array of key/values. try to dump this Nov 27, 2016 at 10:56
  • 1
    Are you using any dropins, like an object cache? We certainly had some strange results metadata results using a Redis object cache (our Redis setup was buggy, not the drop-in code). Also, as per @benoti below, try hard-coding $post_id to 87 to confirm if get_the_ID() is working as it should.
    – haz
    Dec 5, 2016 at 4:07

5 Answers 5

1

Not 100% sure I understand the question or problem, but this ought to work, I think:

add_action( 'the_post', 'output_my_custom_field');

function output_my_custom_field( $post_object ) {

    $post_id = $post_object->ID;

    // why the conditional (and redundant !empty) ?
    // if ( !empty($post_id) ) ) { 

        $my_custom_field = get_post_meta( $post_id, 'my_custom_field', true );

        var_dump($my_custom_field);

    // see above
    // }  

 }
1
  • Wondering whether this solution has been tried or not.
    – CK MacLeod
    Dec 10, 2016 at 19:17
0

In a function context, output_my_custom_field() can not use template tag like get_the_ID(), and it returns null. In a function, external data must be provide via the function parameters output_my_custom_field($post) or set with global global $post .

When using the_post action, you need to add $post_object parameter to the function. See the_post example

the_post is only use Iterate the post index in the loop. If you want to display a custom field value, you can use the_content or create a shortcode.

A simple to return the custom .field before the content.

add_filter( 'the_content', 'output_my_custom_field');

function output_my_custom_field($content) {

     global $post; 

    $post_id = $post->ID;
    $post_title = $post->post_title;

    if ( !empty( $post_id ) ) {
        $my_custom_field = get_post_meta( $post_id, 'my_custom_field', true );

        var_dump($my_custom_field);
    $content = $my_custom_field . $content;
    }   
    return $content;
}

If you want to use template tag in your function, your need to add setup_postdata( $post ); .

    add_filter( 'the_content', 'output_my_custom_field');

    function output_my_custom_field($content) {
       global $post;
    setup_postdata($post);

        $post_id = get_the_ID();

        if ( !empty( $post_id ) ) {
             $my_custom_field = get_post_meta( $post_id, 'my_custom_field', true );

            var_dump($my_custom_field);

            wp_reset_postdata();
        }   
     return $content;
    }

In your case there's no need to reset the post data with wp_reset_postdata(), because this function restore the original query (and your running on the same one).

Hope it helps to make it work !

2
  • Thanks. I tried it and still getting NULL. I was actually getting the correct post_id, but even when I tried to hard-code it, still the result is same. Dec 4, 2016 at 19:54
  • I edit some things, action name.... become add_filter
    – Benoti
    Dec 4, 2016 at 20:12
0

This is more of a comment than an answer, but I can't "comment".

Have you checked the posts that you're trying to retrieve this information from?

Are the posts marked as private (admin only) or not published?

Try creating another WordPress account with limited access (but higher rights than a regular user) and see if it works.

1
  • Did you find a solution to this problem? Dec 11, 2016 at 20:08
0

It's just the hook issue you are using 'the_post', try an other one which suits here, like I just tried using 'init' and its working just fine.

add_action( 'init', 'output_my_custom_field');
function output_my_custom_field() {
    $post_id = get_the_ID();

    if ( !empty( $post_id ) ) {
        $my_custom_field = get_post_meta( $post_id, 'my_custom_field', true );

        var_dump($my_custom_field);
    }   
}
1
  • Tried it, not working. Dec 6, 2016 at 17:23
0

Try:

$my_custom_field = get_post_meta( $post_id, 'my_custom_field', false );
var_dump($my_custom_field);

It could be that you created the meta field with the wrong version of add_post_meta( $post_id, 'my_custom_field', 'IT works!', false )

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.