How do I get the post meta?

#1

Context:
I have a meta field on all my posts called ‘_user_id’. this stores the user who own’s the post (different than the post author).

Task:
I’m trying to show a button on every post in the post module that, when clicked, opens a pop up with a form. This form takes the meta value in _user_id and shoots of an email.

Attempt:
I’ve copied the posts module into my theme. I’ve created a simple “show button” setting in post-grid.php with various options.

I’ve created this function at the end of the post-grid.php file:

function contact_button($settings, $post){
	if(!empty($settings->show_contact) && $settings->show_contact == '1'){
		echo '<div class="fl-profile-contact-wrapper" align="'.(!empty($settings->contact_alignment) ? $settings->contact_alignment : 'left' ).'"><input type="button" class=\'fl-profile-contact-button fl-button\' data-toggle="quickview" data-toggle-element="#quickview" data-user-id="'.($post->_user_id).'" value="'.(!empty($settings->contact_text) ? $settings->contact_text : '' ).'" /></div>';
	}
}

Then I use this to display the button in the grid, gallery and feed views:


<?php contact_button($settings, $post); ?>

The button shows up fine. However, when clicked, the data-user-id is not showing up in the inspector.

I could probably somehow store the value in $settings but that doesn’t seem right. What am I missing?

#2

Figured it out! I needed to use

<php contact_button($settings, $user_id = get_post_meta(get_the_ID(), '_user_id', true) ); ?>

and then make sure $user_id was the argument to my function and echoed in my button html.

function contact_button($settings, $user_id){
//etc..
#3

Glad you figured it out, Thomas! And thanks for taking the time to let us know as well as sharing the solution. Enjoy! :slight_smile:

Ben

closed #4