当我在进行wp查询时,我正在丢失帖子ID。我需要在我插入记录的地方,以及之前如果它找到现有记录,因为我用它来查找附加到帖子的自定义字段。我在这里使用页面模板和高级自定义字段。
发布ID丢失
<?php
/**
* Template Name: Well Being Page Template
*
* @package WordPress
* @subpackage Twenty_Fourteen
* @since Twenty Fourteen 1.0
*/
?>
<?php get_header(); ?>
<?php
$ip = getenv('HTTP_CLIENT_IP')?:
getenv('HTTP_X_FORWARDED_FOR')?:
getenv('HTTP_X_FORWARDED')?:
getenv('HTTP_FORWARDED_FOR')?:
getenv('HTTP_FORWARDED')?:
getenv('REMOTE_ADDR');
// args
$args = array(
'numberposts' => -1,
'post_type' => 'wellbeing',
'post_status' => 'publish',
'meta_key' => 'ip',
'meta_value' => $ip
);
// query
$the_query = new WP_Query( $args );
// I HAVE THE POST ID HERE SO THAT I CAN REFERENCE IT IF THERE IS A RECORD FOUND USE THE POST ID FROM THE POST ?
$isFound=false;
?>
<?php
$count = $the_query->found_posts;
while ( $the_query->have_posts() ) : $the_query->the_post();
if(count==1)
{
$dateJoined = new DateTime($dateJoined);
$expiredate = new DateTime($expiredate);
$diff = $dateJoined->diff($expiredate);
echo 'Days to end'.$diff->days;
}
endwhile;
if ($count==0)
{
// Gather post data.
$my_post = array(
'post_title' => 'Opt In',
'post_content' => 'This is my post.',
'post_type' => 'wellbeing',
'meta_key' => 'ip',
'meta_value' => $ip,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 )
);
// Insert the post into the database.
$post_id = wp_insert_post( $my_post );
// I HAVE THE POST ID HERE OK BUT I ALSO NEED IT ABOVE AT THE WP_QUERY
$date = new DateTime();
$date->modify('+2 week');
$currentdate = date('m/d/Y h:i:s', time());
// save a basic text value
$field_key = "expirey_date";
$twoweeksaway = $date->format('m/d/Y h:i:s');;
update_field( $field_key, $twoweeksaway, $post_id );
// save a basic text value
$field_key = "date_joined";
$value = $currentdate;
update_field( $field_key, $value, $post_id );
$current_user = wp_get_current_user();
// save a basic text value
$field_key = "ip";
$value = $ip;
update_field( $field_key, $value, $post_id );
// save a basic text value
$field_key = "email";
$value = wpse_email();
update_field( $field_key, $value, $post_id );
$dateJoined=the_field('date_joined', $post_id );
$expiredate=the_field('expirey_date', $post_id );
$dateJoined = new DateTime($dateJoined);
$expiredate = new DateTime($expiredate);
$diff = $dateJoined->diff($expiredate);
echo 'Days to end'.$diff->days;
}
?>
<?php
echo 'Post Id'.$post_id;
$dateJoined=the_field('date_joined', $post_id );
$expiredate=the_field('expirey_date', $post_id );
//Convert them to timestamps.
$difference=(int)abs((strtotime($expiredate) - strtotime($dateJoined))); // 3
?>
<STYLE>
.myCheckbox input {
// display: none;
// Better than display: none for accessibility reasons
position: relative;
z-index: -9999;
}
.myCheckbox span {
width: 20px;
height: 20px;
display: block;
background: url("link_to_image");
}
.myCheckbox input:checked + span {
background: url("link_to_another_image");
}
</STYLE>
<div style="clear:both;">
Welcome this page allows you to opt out of our well being program. You have <?php echo $difference.'day(s) to opt out click opt out button below';?>
Do you wish to opt out of the well being program
</div>
<div class="form-group">
<form name="wellbeing" action="<?php echo get_permalink(); ?>" method="post">
<input type="submit" value="Opt Out"/>
</div>
</form>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
<?php get_footer(); ?>