How do I get the post meta?

About this forum
These forums have not been active for several years. They're available for search and reference, but a lot of this information is likely out of date.

For current information, try visiting our Knowledge Base.

Home / Support / Custom Module Development / How do I get the post meta?

This topic is: resolved
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #103899

    m33
    Participant

    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?

    #103923

    m33
    Participant

    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..
    #104085

    Ben Carlo
    Keymaster

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

    Ben

Viewing 3 posts - 1 through 3 (of 3 total)

The forum ‘Custom Module Development’ is closed to new topics and replies.