验证meta_key是否已存在

时间:2014-03-27 07:29:14

标签: php wordpress

我正在尝试验证wp_postmeta中的meta_key是否已经存在,如果metakey已经存在,它将使用update_post_meta,否则它将使用add_post_meta。我不能让它工作,似乎与我的代码,任何帮助的逻辑错误?

if(isset($_POST['submitHoliday']))
{

    global $wpdb;

        $holiday = $_POST['holiday'];
        $cMonth = $_POST['country_month'];
        $cYear = $_POST['sortYear'];

        $timestamp = mktime(0,0,0,$cMonth,1,$cYear);
        $maxday = date("t",$timestamp);
        $thismonth = getdate ($timestamp);
        $startday = $thismonth['wday'];

        for ($i=0; $i<$maxday+$startday; $i++) 
        {

            if($i < $startday) continue;

            $date = $cYear."-".$cMonth."-".($i-$startday+1);
            $event = $holiday[$i-$startday];

            if($event != NULL AND !get_post_meta($_POST['country'], $date))
            {

               add_post_meta($_POST['country'], $date, $event);

            }
            else
            {
                update_post_meta($_POST['country'], $date, $event);
            }
        }   

}

0 个答案:

没有答案