Save post meta and reuse them

So I have two files (Read the “Edit: Clarification” at the bottom first)

first.php:

Read More
<div class="rfp_hide " >
    <?php   
    $args = array( 
        'post_type' => 'post',
        'paged'=>$paged,
        'posts_per_page' => 15,         
        'orderby' => 'date',            
        'order' => 'DESC'
        );
    $loop = new WP_Query( $args );
    $id = get_the_ID();     
    while ( $loop->have_posts() ) : $loop->the_post(); 
    ?>

     <?php the_title(); ?>
     <?php the_content(); ?>
     <?php echo '<button class="button" data-post_id="' .$id. '">' ;?>
         <?php echo get_post_meta($post->ID, 'rh_type', true); ?>   
     <?php echo '</button>';?>  
     <div class="new_content">
          <!--Ajax new content goes here-->
     </div>
 </div>

So this will display posts with title, content, button with a custom meta (“rh_type”). When the button is clicked, it loads another file (second.php) via ajax as below within new_content div.

Second.php

 <div class="second_content">
     <?php the_title(); ?>
     <?php the_content(); ?>
     <?php echo get_post_meta($post->ID, 'rh_type', true); ?>   
 </div>

In other words, when the button is clicked from the first.php, then the second.php will be loaded within the new_content div.
Once it is loaded, I want to make it so that the same title, content, and the custom meta as the first.php are displayed.

However here is the problem.

Because the meta in the second.php are not originally part of the loop (meaning, it is outside of the loop), even though it is loaded within the loop once the button clicked, it does not display any of the post meta.

I am not sure what the best approach is to “extend” the loop, here is my approach.

APPROACH:

  1. Every posts will have its own unique post_id, and buttons will also have the post_id with them.
  2. When the button is clicked, save the post_id (I am guessing with jQuery) along with other meta data (title, etc).
  3. Once the second.php is loaded, then load or paste these saved meta data to an appropriate place.

This approach might work, but I am not sure how to accomplish them.

Any help will be much appreciated it.

Thanks!

EDIT: Clarification

The question above is simplified version of what I have, in order to avoid the confusion.

So, here is what I am trying to achieve.

  1. There are 10 posts displayed in a page.
  2. Each posts have a button to call “contact_form.php” via ajax.
  3. Once the button is clicked, then the contact form is shown.
  4. The contact form has typical input fields such as name, email, etc.
  5. Once these are filled out, then is submitted.

So far so good.

When I receive the email, then the problem occurs.

I have no idea which post the contact form was sent from.

So, my approach (or trying to make it work) is to somehow save the post meta of which the button was clicked, then “paste” these data onto the contact form, so that when I receive the email, I know which content it is from.

Related posts

1 comment

Comments are closed.