简体   繁体   English

WordPress如何在循环外获取帖子ID

[英]Wordpress how to get post ID outside of the loop

I have a custom post type called 'bookings' that saves several fields of meta data to wp_postmeta. 我有一个名为“ bookings”的自定义帖子类型,该类型将元数据的多个字段保存到wp_postmeta。

I have a page that displays all the entries from this custom post type, ie all the bookings. 我有一个页面显示此自定义帖子类型的所有条目,即所有预订。

Here is the code to display my bookings: 这是显示我的预订的代码:

<?php 
$current_user = wp_get_current_user();
$args = array(
'post_type' => 'bookings',
'meta_query' => array(
    array(
        'key' => 'wedding_user',
        'value' => $current_user->display_name,
        'compare' => '=',
    )
)
);
// Welcome
echo '<div class="user_avatar">'.get_avatar( $current_user->ID, 32 ).'</div>';
echo '<div class="user_welcome"><p>Hello '.$current_user->display_name.' <span class="user_id">(ID# '.$current_user->ID.')</span></p>';

// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
    $the_query->the_post();

    if (get_post_meta( $the_query->post->ID, 'wedding_hidden_is_completed', true )==1) {
        $is_complete = 'Form Complete <br /><span class="small">('.get_post_meta( $the_query->post->ID, 'wedding_hidden_date_completed', true ).')</span>';
    } else {
        $is_complete = 'Not Complete';
    }

    echo '<p class="booking_id">Booking ID: DISTR' . $the_query->post->ID . '</p>';
    echo '<ul class="show_bookings">';
    echo '<li>' . get_post_meta( $the_query->post->ID, 'wedding_name', true ) . '</li>';
    echo '<li>' . get_post_meta( $the_query->post->ID, 'wedding_date', true ) . '</li>';
    echo '<li>' . get_post_meta( $the_query->post->ID, 'wedding_package', true ) . '</li>';
    echo '<li>£' . get_post_meta( $the_query->post->ID, 'wedding_price', true ). '</li>';
    echo '<li>' . get_post_meta( $the_query->post->ID, 'wedding_payment_due_date', true ) . '</li>';
    echo '<li>' . $is_complete . '</li>';
    echo '<li>Is Viewed?</li>';
    echo '<li>Actions';
    echo '<ul class="actions-sub-menu">';
    echo '<li><a href="'. esc_url(add_query_arg( 'booking-id', $the_query->post->ID, site_url( '/booking-form/' ) )).'">Fill out booking form</a></li>';
    echo '<li><a href="'. esc_url(add_query_arg( 'booking-id', $the_query->post->ID, site_url( '/pay-deposit/' ) )) .'">Pay deposit</a></li>';
    echo '<li>Pay remaining balance</li>';
    echo '<li>Email a copy of your receipt</li>';
    echo '<li>View booking form</li>';
    echo '</ul>';
    echo '</li> <!--end actions-sub-menu-->';
    echo '</ul>';

    ?>
<div class="pay_deposit">
<?php
require DISTRACTIONS_LOGIN_PLUGIN_DIR . 'includes/payments/deposit.php'; 
?>
</div> <!-- end pay deposit -->

<?php

}
} else {
echo '<p>No bookings found</p>';
}
/* Restore original Post Data */
wp_reset_postdata();

?>

under each booking is a paypal payment button - it uses paypal IPN, so nothing too complex (you will see the required file in the "pay_deposit" div). 每个预订下都有一个Paypal付款按钮-它使用Paypal IPN,所以没有什么太复杂的(您将在“ pay_deposit” div中看到所需的文件)。

Here is my paypal payment form: 这是我的贝宝付款表格:

<?php
$paypal_url='https://www.sandbox.paypal.com/cgi-bin'; // 
$paypal_id='malik@thedistractionsband.co.uk'; // Business email ID
$booking_form_id = $the_query->post->ID;
$total_amount = get_post_meta( $the_query->post->ID, 'wedding_price', true );
$deposit_amount = $total_amount*0.2;
?>

<h3>Pay Deposit</h3>
<p>Your deposit aount of £<?php echo $deposit_amount; ?> ...</p>      
<form action="<?php echo $paypal_url; ?>" method="post" name="frmPayPal1">

<input type="hidden" name="business" value="<?php echo $paypal_id; ?>">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="item_name" value="<?php echo get_post_meta( $the_query->post->ID, 'wedding_name', true ); ?> - 20% Deposit">
<input type="hidden" name="item_number" value="DISTR<?php echo $booking_form_id; ?>">
<input type="hidden" name="credits" value="510">
<input type="hidden" name="userid" value="<?php echo $current_user->ID; ?>">
<input type="hidden" name="amount" value="<?php echo $deposit_amount; ?>">
<input type="hidden" name="cpp_header_image" value="http://www.thedistractionsband.co.uk/files/2015/08/LOGO-1.1-1024x304.png">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="handling" value="0">
<input type="hidden" name="cancel_return" value="<?php echo get_site_url()."/payment-cancel/"; ?>">
<input type="hidden" name="return" value="<?php echo get_site_url()."/my-bookings/"; ?>"> 
<input name="notify_url" value="<?php echo DISTRACTIONS_LOGIN_PLUGIN_URL ?>includes/payments/paypal-ipn.php" type="hidden">

<input type="submit" border="0" name="submit" value="Pay Now" alt="PayPal - The safer, easier way to pay online!">

<div class="cards"><i class="fa fa-cc-amex"></i> <i class="fa fa-cc-mastercard"></i> <i class="fa fa-cc-visa"></i> <i class="fa fa-credit-card"></i> <i class="fa fa-cc-paypal"></i></div>
</form>

When a payment is made, it uses the following file to communicated with paypal and update my database - paypal-ipn.php 付款后,它将使用以下文件与Paypal通信并更新我的数据库-paypal-ipn.php

<?php
// Make wordpress functions available - so we can write to the db etc
$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
require_once( $parse_uri[0] . 'wp-load.php' );

global $wpdb;
update_post_meta($booking_id, 'deposit_paid', 1); 

?>

My issue is in this file. 我的问题在此文件中。 The update_post_meta function is used to add a '1' to the deposit_paid column in my wp_postmeta db table. update_post_meta函数用于在我的wp_postmeta db表中的deposit_paid列中添加“ 1”。 The issue I have is, how do I define the $booking_id - making sure that it is for the booking that has been paid for. 我的问题是,如何定义$ booking_id-确保它用于已付款的预订。

For example, on my page that lists all my bookings, if the first has post_id 10 and I pay for that, how do I make sure that the 'deposit_paid' entry for post_id 10 is updated? 例如,在列出我所有预订的页面上,如果第一个预订有post_id 10,并且我为此付款,我如何确保post_id 10的“ deposit_paid”条目已更新?

Figured it out, I need to use: 想通了,我需要使用:

$booking_id = $_POST['item_number'];

To get the item number sent to paypal (which is the same as the post id) 要获取发送到贝宝的商品编号(与邮寄编号相同)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM