Post meta data empty on frontend (gct_get_post_custom doesn't accept params)
-
Symptom: In some situation, gc Testimonials widget is unable to retrieve and display post meta like client_name, company_name etc.
Cause: Function
gct_get_post_custom()
tries to get custom post metas for the post, using the global$post
variable, but the global$post
can contain data coming from any post type, depending on the contest, and not only from GC_testimonial posts.
This function it’s called everywhere passing in the post ID as parameter but it doesn’t accept parameters. So, when the global$post
contain something else than a GC_testimonial post, the function can’t retrieve custom meta as client_name, company_name etc etc.Solution: To fix this issue, I’ve just added a param in the function definition and then passed it to get_post_custom. Just go to the last function in testimonials.php and substitute it with the following:
function gct_get_post_custom($post_id) { global $post; //eventually get post id from the current query if (empty($post_id)) $post_id = $post->ID; $custom = get_post_custom($post_id); $fields = array( 'client_name', 'client_photo', 'company_name', 'company_website', 'email', ); foreach ( $fields as $field ) { if ( ! isset( $custom[ $field ][ 0 ] ) ) $custom[ $field ][ 0 ] = ''; } return $custom; }
- The topic ‘Post meta data empty on frontend (gct_get_post_custom doesn't accept params)’ is closed to new replies.