Is it possible to get a post ID of a 2 different custom post types loop embedded in the same post to perform update_post_meta from the frontend?

I have a ‘Campaign’ custom post type that has details (a paragraph or so) of a particular cause.

On the same post exists a ‘Donations’ form (to post user details, donation amount etc) for payments processing that updates the Donations custom post type fmeta from the front end.

However to update the Donations post meta it needs to get that the global $post_id of the Donations post type yet its picking the Campaign Post Type.

Here is the form code:

<?php
if (isset( $_POST[‘cpt_nonce_field'] ) && wp_verify_nonce( $_POST[‘cpt_nonce_field'],‘cpt_nonce_action' ) && ($post_id) ) 
{
update_post_meta($post_id, donations_metabox_donation-id, $_POST['donation_id']);
update_post_meta($post_id, donations_metabox_cause-id, $_POST['cause_id']);
update_post_meta($post_id, cause-title, $_POST['donation_for']);
update_post_meta($post_id, donations_metabox_donation-currency, $_POST['donation_currency']);
update_post_meta($post_id, donations_metabox_cause-amount, $_POST['cause_amount']);
update_post_meta($post_id, donations_metabox_full-name, $_POST['full_name']);
update_post_meta($post_id, donations_metabox_email, $_POST['email']);
}
?>

<?php get_header(); ?>

<!-- main_wrapper -->
<div id="main_wrapper">
<div class="content-wrapper">

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<!-- tbl-layout -->
<div class="tbl-layout">
<div class="col left">
    <h1><?php the_title(); ?></h1>

    <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
    <p><?php the_content(); ?></p>
</div>

<?php endwhile; ?>

<?php endif; ?>

<div class="col right">
<h2>Your Donation</h2>
<!-- donation start -->
<form method="post" action="">
<label for="donation_for">
I would like to donate to
</label><br>
<select name="donation_for" id="donation_for" class="dd">
<?php

$args = array( 'post_type'=>'cause','posts_per_page' => -1,);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
?>
<option value="<?php the_title(); ?>"><?php the_title(); ?></option>
<?php endforeach; ?>
</select>

<br>

<label for="donation_currency">Currency</label><br>
<select name="donation_currency[]" id="donation_currency" class="dd">
<option value="USD">USD</option>
<option value="KES">KES</option>
<option value="ZAR">ZAR</option>
</select>

<br><br>

<label for="cause_amount">Donation Amount</label><br>
<input type="text" class="textbox" id="cause_amount" name="cause_amount" placeholder="Enter Amount" maxlength="100" autocomplete="off" />

<br>

<br>

<label for="full_name">Full Name</label><br>
<input type="text" class="textbox" id="full_name" name="full_name" placeholder="Enter Full Name" maxlength="100" autocomplete="off" />

<br>

<br>

<label for="email">Email</label><br>
<input type="email" class="textbox" id="email" name="email" placeholder="Enter Email" maxlength="100" autocomplete="off" />

<br><br><br>

<button type="submit" class="btn-donate">Donate Now</button>

<input type="hidden" name="donation_id" id="donation_id" value="<?php printf(uniqid()); ?>">

<input type="hidden" name="cause_id" id="cause_id" value="<?php the_ID();?>">

<?php wp_reset_postdata(); ?>


<?php wp_nonce_field( ‘cpt_nonce_action', ‘cpt_nonce_field' ); ?>


</form>
</div>
<!-- donation end -->
</div>
</div>
<!-- tbl-layout end -->                 

</div>
</div>
<!-- main_wrapper end -->

<?php get_footer(); ?>

And here is the donation metabox code:

/**
* Generated by the WordPress Meta Box Generator 
*/
class Donation_Meta_Box {
private $screens = array(
'donation',
);
private $fields = array(
array(
    'id' => 'donation-id',
    'label' => 'donation_id',
    'type' => 'number',
),
array(
    'id' => 'cause-id',
    'label' => 'cause_id',
    'type' => 'number',
),
array(
    'id' => 'cause-title',
    'label' => 'cause_title',
    'type' => 'text',
),
array(
    'id' => 'donation-currency',
    'label' => 'donation_currency',
    'type' => 'text',
),
array(
    'id' => 'cause-amount',
    'label' => 'cause_amount',
    'type' => 'number',
),
array(
    'id' => 'full-name',
    'label' => 'full_name',
    'type' => 'text',
),
array(
    'id' => 'email',
    'label' => 'email',
    'type' => 'email',
),
/*array(
    'id' => 'donation-permissions',
    'label' => 'donation_permissions',
    'type' => 'text',
),
array(
    'id' => 'subscription',
    'label' => 'subscription',
    'type' => 'text',
),*/
);

/**
* Class construct method. Adds actions to their respective WordPress hooks.
*/
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_post' ) );
}

/**
* Hooks into WordPress' add_meta_boxes function.
* Goes through screens (post types) and adds the meta box.
*/
public function add_meta_boxes() {
foreach ( $this->screens as $screen ) {
    add_meta_box(
        'donations-metabox',
        __( 'Donations Metabox', 'amref_2017' ),
        array( $this, 'add_meta_box_callback' ),
        $screen,
        'advanced',
        'default'
    );
}
}

/**
* Generates the HTML for the meta box
* 
* @param object $post WordPress post object
*/
public function add_meta_box_callback( $post ) {
wp_nonce_field( 'donations_metabox_data', 'donations_metabox_nonce' );
echo 'Donations Metabox Boilerplate';
$this->generate_fields( $post );
}

/**
* Generates the field's HTML for the meta box.
*/
public function generate_fields( $post ) {
$output = '';
foreach ( $this->fields as $field ) {
    $label = '<label for="' . $field['id'] . '">' . $field['label'] . '</label>';
    $db_value = get_post_meta( $post->ID, 'donations_metabox_' . $field['id'], true );
    switch ( $field['type'] ) {
        default:
            $input = sprintf(
                '<input %s id="%s" name="%s" type="%s" value="%s">',
                $field['type'] !== 'color' ? 'class="regular-text"' : '',
                $field['id'],
                $field['id'],
                $field['type'],
                $db_value
            );
    }
    $output .= $this->row_format( $label, $input );
}
echo '<table class="form-table"><tbody>' . $output . '</tbody></table>';
}

/**
* Generates the HTML for table rows.
*/
public function row_format( $label, $input ) {
return sprintf(
    '<tr><th scope="row">%s</th><td>%s</td></tr>',
    $label,
    $input
);
}
/**
* Hooks into WordPress' save_post function
*/
public function save_post( $post_id ) {
if ( ! isset( $_POST['donations_metabox_nonce'] ) )
    return $post_id;

$nonce = $_POST['donations_metabox_nonce'];
if ( !wp_verify_nonce( $nonce, 'donations_metabox_data' ) )
    return $post_id;

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    return $post_id;

foreach ( $this->fields as $field ) {
    if ( isset( $_POST[ $field['id'] ] ) ) {
        switch ( $field['type'] ) {
            case 'email':
                $_POST[ $field['id'] ] = sanitize_email( $_POST[ $field['id'] ] );
                break;
            case 'text':
                $_POST[ $field['id'] ] = sanitize_text_field( $_POST[ $field['id'] ] );
                break;
        }
        update_post_meta( $post_id, 'donations_metabox_' . $field['id'], $_POST[ $field['id'] ] );
    } else if ( $field['type'] === 'checkbox' ) {
        update_post_meta( $post_id, 'donations_metabox_' . $field['id'], '0' );
    }
}
}
}
new Donation_Meta_Box;

I’ll use the Campaign’s id to establish the relationship between the two post types so i’m not using plugins such as Post 2 Post or Pods (unless advised its necessary based off my problem)

First time poster, thanks for your time.

Read more here: Is it possible to get a post ID of a 2 different custom post types loop embedded in the same post to perform update_post_meta from the frontend?

Leave a Reply

Your email address will not be published. Required fields are marked *