• Alright, I’m stumped and feeling stupid.

    I have this line of code:
    <?php echo ‘Course ID: ‘ . get_post_meta(17234, ‘_lesson_course’, true); ?>

    I’ve stripped it out of a larger function, but I can’t even get this simple one to work. I’ve found the item in the database (screenshot: https://snag.gy/1IKwl4.jpg)

    But it just returns back:
    Course ID:

    In other words, no value for the _lesson_course meta data.

    I’ve double checked, there’s only one record, so it’s not an issue of pulling the wrong one. I’m stumped. Any ideas?

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    The leading underscore is an informal “private” designation which indicates we should not be directly using the value. There’s normally no technical reason you couldn’t do so anyway. It’s possible the plugin that established the field is also somehow hooked into get_post_meta() in a way that prevents us from getting the value.

    Try placing your test code temporarily on a twentyseventeen template file, then install and activate the health-check plugin. Activate troubleshooting mode in the plugin. In that mode, make a request involving the template with your test code. If it works through this mode, there’s either some sort of conflict or intentional code preventing your code from working.

    You can activate plugins one by one using the troubleshooting item in the admin bar. When your code again fails will tell you if it’s a conflict or intentional. If you can narrow down the source to the precise code responsible, it can be circumvented.

    You could also try getting the value directly through an SQL query. Also, if you are trying to get this value immediately after it’s first saved, you may have a race condition where your code executes before the value can be written to the DB.

Viewing 1 replies (of 1 total)
  • The topic ‘get_post_meta not returning value’ is closed to new replies.