邮件标题的get_post始终返回上次插入的帖子

时间:2017-04-12 22:43:21

标签: wordpress woocommerce

当我尝试在wordpress中使用get_post时,它总是返回最后一篇文章。

$the_code = 'couponcode';
$args = array(
  'post_title'  => 'couponcode',
  'post_type'   => 'shop_coupon',
  'post_status' => 'publish',
  'numberposts' => 1
);
$my_code = get_posts($args);

这是我插入优惠券的方式:

$coupon = array(
    'post_title' => 'couponcode',
    'post_content' => '',
    'post_status' => 'publish',
    'post_author' => 1,
    'post_type'     => 'shop_coupon'
);

$new_coupon_id = wp_insert_post( $coupon );

它在后端管理员中显示正常。

2 个答案:

答案 0 :(得分:2)

尝试这个简单的查询。

$posttitle = 'testcoupon';
$coupons = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_title = '" . $posttitle . "' AND post_type = 'shop_coupon'" );

此代码已经过测试确定

答案 1 :(得分:1)

根据您的要求,您可以尝试这个

$coupontitle = 'couponcode';
$postid = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '" . $coupontitle . "'" );
相关问题