我试图在比赛的第一个条目上创建一个新的帖子,然后在后续条目上更新现有条目,这样我可以每个条目有一个帖子,但允许多个条目。
问题是,如果你参加比赛(见下面的链接),它将创建一个新的帖子(它有效)。然后,如果你刷新页面,它会说你需要等待X分钟(在这种情况下是5分钟),然后才能再次输入(它可以工作)。
但是,如果您返回到条目页面,使用相同的电子邮件和相同的zip,或相同的电子邮件和不同的zip再次输入,它有时会做正确的事情并更新帖子条目,但有时会创建一个新条目(新帖子),而不是更新现有条目。
我需要帮助让它做正确的事情。如果条目存在,则更新该条目;如果条目不存在,则创建一个新条目。
代码:
// SYSTEM VARS
$currentTime = time();
$entryCode = md5($entryEmail);
// ENTRY DATA
$entryEmail = sanitize_email($_POST[\'entryEmail\']);
$entryZip = sanitize_text_field($_POST[\'entryZip\']);
$entryIP = $_POST[\'entryIP\'];
// CREATE ENTRY
function createEntry($cee, $cez, $ceip, $cec) {
$newEntry = wp_insert_post(
array(
\'comment_status\' => \'closed\',
\'ping_status\' => \'closed\',
\'post_author\' => \'1\',
\'post_title\' => $cee,
\'post_status\' => \'publish\',
\'post_type\' => \'custom_type_entries\'
)
);
wp_insert_post($newEntry);
update_post_meta($newEntry, \'entryCode\', $cec, true);
update_post_meta($newEntry, \'entryIP\', $ceip, true);
update_post_meta($newEntry, \'entryZip\', $cez, true);
update_post_meta($newEntry, \'entryTime\', time(), true);
update_post_meta($newEntry, \'entryLast\', time(), true);
update_post_meta($newEntry, \'entryNumber\', \'1\', true);
update_post_meta($newEntry, \'entryWinner\', \'0\', true);
update_post_meta($newEntry, \'entryWinnerTime\', \'0\', true);
update_post_meta($newEntry, \'entryEmail\', $cee, true);
update_post_meta($newEntry, \'entryEmailSubscribe\', \'0\', true);
}
function writeEntry($we) {
$entryFile = "entries.txt";
$dataFile = fopen($entryFile, \'a\');
$entryData = $we . "\\n";
fwrite($dataFile, $entryData);
fclose($dataFile);
}
function secondsToTime($seconds) {
$dtF = new DateTime("@0");
$dtT = new DateTime("@$seconds");
return $dtF->diff($dtT)->format(\'%a days, %h hours, %i minutes and %s seconds\');
}
// CHECK TO SEE IF ALREADY ENTERED
$loop = new WP_Query(
array(
\'post_type\' => \'custom_type_entries\',
\'posts_per_page\' => \'-1\' )
);
while ( $loop->have_posts() ) : $loop->the_post();
$postID = $post->ID;
$entry = get_post_meta($postID, \'entryEmail\', true);
$day = "300";
$entryTime = get_post_meta($postID, \'entryTime\', true);
$entryLastTime = get_post_meta($postID, \'entryLast\', true);
$entryCheckIP = get_post_meta($postID, \'entryIP\', true);
$entryCheckTime = $currentTime - $entryTime;
$entryCheckLastTime = $currentTime - $entryLastTime;
if ($entry == $entryEmail) {
// IF LESS THAN 24 HOURS SINCE FIRST ENTRY--
if ($entryCheckTime <= $day) {
$entryNext = $day - $entryCheckTime;
echo "Time to Second Entry: " . secondsToTime($entryNext);
get_footer();
exit;
}
if ($entryCheckTime >= $day) {
// IF LESS THAN 24 HOURS SINCE LAST ENTRY--
if ($entryCheckLastTime <= $day) {
$entryNext = $day - $entryCheckLastTime;
echo "Time to Next Entry: " . secondsToTime($entryNext);
get_footer();
exit;
}
// IF MORE THAN 24 HOURS SINCE LAST ENTRY--
elseif ($entryCheckLastTime >= $day) {
// UPDATE ENTRY COUNTER
$entryNumber = get_post_meta($postID, \'entryNumber\', true);
$entryNumber++;
update_post_meta($postID, \'entryNumber\', $entryNumber);
// UPDATE ENTRY TIME
update_post_meta($postID, \'entryLast\', $currentTime);
// WRITE ENTRY TO FILE
writeEntry($entryEmail);
// WRAP IY UP YO..
echo "Entry Updated!";
get_footer();
exit;
}
}
}
else {
// CREATE ENTRY
createEntry($entryEmail, $entryZip, $entryIP, $entryCode);
// WRITE ENTRY EMAIL TO FILE
writeEntry($entryEmail);
// WRAP IT UP YO..
echo "New Entry!";
get_footer();
exit;
}
endwhile;
wp_reset_query();
现场示例:
提交表格:weeklyweedbox.com/win