这很奇怪
我有这个代码工作,但后来我需要我的数据库中的一些数据,以确保我不再创建相同的帖子标题,因为如果它不唯一我想生成另一个。
我将使用我添加的内容发布代码并使wp_insert_post停止。 但无论我在哪里搜索,我发现人们应该包括他们:
<?php
define( 'SHORTINIT', true );
//local
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wordpress/wp-load.php' );
//live
//require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
global $wpdb;
$subEmail = isset($_POST['subEmail']) ? $_POST['subEmail'] : null;
if($subEmail){
//Check if $subEmail is in DB
$dbEmails = $wpdb->get_results( 'SELECT meta_value FROM wp_postmeta WHERE meta_key = "_field_newsletteremail"' );
foreach($dbEmails as $emailObj){
if($subEmail === $emailObj->meta_value){
echo "exists";
die();
}
}
//else create a coupon for him
$couponCodeValue = $subEmail.time();
$couponCodeValue = str_replace(' ', '', $couponCodeValue);
$couponCodeValue = md5($couponCodeValue);
//Get Existing Coupons we created
$dbCoupons = $wpdb->get_results( 'SELECT post_title FROM wp_posts WHERE post_type = "shop_coupon" AND post_status = "publish"' );
$x = 0;
$y = 6;
$couponCodeSixValue = substr($couponCodeValue, $x , $y);
for($y; $y < strlen($couponCodeValue); $y++){
//if code found generate new one, else just us the current
if($dbCoupons[$i]->post_title === $couponCodeSixValue){
$couponCodeSixValue = substr($couponCodeValue, ($x+1) , ($y+1));
}
$x++;
}
imagineme_create_coupon($couponCodeSixValue);
}
function imagineme_create_coupon( $couponCodeSixValue ){
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wordpress/wp-load.php' );
$coupon_code = $couponCodeSixValue; // Code
$amount = '10'; // Amount
$discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product
$coupon = array(
'post_title' => ''.$coupon_code,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon'
);
$new_coupon_id = wp_insert_post( $coupon );
// Add meta
update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
update_post_meta( $new_coupon_id, 'individual_use', 'yes' );
update_post_meta( $new_coupon_id, 'product_ids', '' );
update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
update_post_meta( $new_coupon_id, 'usage_limit', '1' );
update_post_meta( $new_coupon_id, 'expiry_date', '' );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
echo $coupon_code;
}
?>
在我添加之前,最底层的功能正在运行:
define( 'SHORTINIT', true );
//local
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wordpress/wp-load.php' );
//live
//require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
global $wpdb;
但如果没有这个,我无法从这边的脚本访问wpdb。
如何确保加载wp_insert_post的所有文件并访问它,因为我收到PHP致命错误:未捕获错误:调用未定义函数wp_insert_post()