post meta - get_post_meta returning empty string when data shows in the database

admin2025-01-07  4

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:

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()) {
        $search_posts->the_post();
        $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;
                }
                break;
            case 'course':
                if(sensei_has_user_started_course($post_id, $user_id)) {
                    $allowed_posts[] = $post_id;
                }
                break;
            default:
                $allowed_posts[] = $post_id;
                break;
        }
    }
    wp_reset_postdata();
    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?

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:

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()) {
        $search_posts->the_post();
        $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;
                }
                break;
            case 'course':
                if(sensei_has_user_started_course($post_id, $user_id)) {
                    $allowed_posts[] = $post_id;
                }
                break;
            default:
                $allowed_posts[] = $post_id;
                break;
        }
    }
    wp_reset_postdata();
    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?

Share Improve this question edited Mar 6, 2018 at 17:01 James Meyer asked Mar 6, 2018 at 16:18 James MeyerJames Meyer 414 bronze badges 1
  • 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 Commented Jan 2, 2024 at 16:21
Add a comment  | 

1 Answer 1

Reset to default 0

Edit

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();
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736253458a125.html

最新回复(0)