I'm absolutely stumped and I'm hoping this is something stupid that I just can't see because I'm too close to it.

Working on an LMS using Sensei. Trying to get the course_id from a lesson. Pretty simple, it's just meta data on a custom post type.

Cut everything down to this simple statement:

echo 'Course ID: ' . get_post_meta(17234, '_lesson_course', true);

I checked the database, the data is definitely there: enter image description here

And there's only one match.

But, it always just returns an empty string (or an empty array if I set the final argument to false).

Did some more digging. Found that my issue is my new function, but I don't know why.

Here's the entire mess:

function search_results_per_page( $query ) {
    echo 'Checking';
    if ( $query->is_search() ) {
        $accessible_post_ids = get_accessible_post_ids();
        $query->set('post__in', $accessible_post_ids);
    return $query;
add_action( 'pre_get_posts',  'search_results_per_page'  );

function get_accessible_post_ids() {
    $allowed_posts = [];
    $user_id = get_current_user_id();
    $args = array('posts_per_page' => -1, 'post_type' => array('lesson', 'course', 'page'));
    $search_posts = new WP_Query($args);
    while($search_posts->have_posts()) {
        $post_type = get_post_type();
        $post_id = get_the_id();
        switch($post_type) {
            case 'lesson':
                echo 'Post ID: ' . $post_id;
                $course_id = get_post_meta($post_id, '_lesson_course', true);    
                echo ' Course ID: ' . $course_id . '<br>';
                if(sensei_has_user_started_course($course_id, $user_id)) {
                    $allowed_posts[] = $post_id;
            case 'course':
                if(sensei_has_user_started_course($post_id, $user_id)) {
                    $allowed_posts[] = $post_id;
                $allowed_posts[] = $post_id;
    return $allowed_posts;

For whatever reason, once I'm inside the while loop, get_post_meta no longer works. I sort of expected the while loop to fail, because it seems like it might be an infinitely re-occuring loop with the filter, but it seems to run. Just the get_post_meta returns an empty string.

Any brilliant thoughts out there?

  • Is it possible to change how you're doing your query? For example, instead of sub-querying for Post IDs, you add a meta_query and check for if a relationship exists?
    – T Paone
    Jan 2 at 16:21

1 Answer 1



Invalid Answer

The answer below is invalid as get_the_id() and get_the_ID() both return the required ID of the post inside the Loop. Thanks to @Jess_Pinkman for pointing it out.

Your code should be working fine, but the only issue is you are using an incorrect function to get the ID of the post inside the Loop.

To get the ID of the post inside the loop, you need to use the function get_the_ID().

What your are using is get_the_id() when, in reality, it's get_the_ID(). There's a lot of difference in that.

$post_id = get_the_id(); // This will not return anything since the
                         // function doesn't even exist, so $post_id
                         // will be empty.

Since get_the_id() doesn't return anything, the $post_id variable remains empty. So when the get_post_meta() function is run, it doesn't know which post's meta to get as there's no given ID. If you enabled WordPress Debug Mode, it would even give you a few errors.

To get the meta, you would need to use the correct function, i.e. get_the_ID() as given below.

$post_id = get_the_ID();
  • Please edit your answer and add more details in how this is a solution to the question. Thanks!
    – Christine Cooper
    Oct 15, 2019 at 13:17
  • 1
    @ChristineCooper Thank you for the kind reminder. I have edited the answer, and you can check it.
    – Anteraez
    Oct 15, 2019 at 20:11
  • @Anteraez That was was also my first thought, but i tried get_the_id() and it actually works. Oct 16, 2019 at 1:35
  • @Jess_Pinkman Thanks for letting me know. I checked it, and as you said, get_the_id() does work. My answer is invalid now.
    – Anteraez
    Oct 16, 2019 at 13:08

Your Answer

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

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