php 如何使用wp_insert_post防止重复发布?

kdfy810k  于 2023-06-21  发布在  PHP
关注(0)|答案(2)|浏览(118)

如何防止重复的职位与wp_insert_post上single.php?
我的准则是

$post_id = wp_insert_post( array(
  'post_status' => 'publish',
  'post_type' => 'post',
  'post_title' => 'Test Shop1',
  'post_content' => 'Lorem ipsum'
) );

$post_type = 'shop';
$query = "UPDATE {$wpdb->prefix}posts SET post_type='".$post_type."' WHERE id='".$post_id."' LIMIT 1";
GLOBAL $wpdb; 
$wpdb->query($query);

但是每次我刷新它都会添加重复的帖子..我该如何防止这种情况?请帮帮忙

5cg8jx4n

5cg8jx4n1#

$post_title = 'Test Shop1';
if (!post_exists($post_title)) { // Determine if a post exists based on title, content, and date
    $post_id = wp_insert_post(array(
        'post_status' => 'publish',
        'post_type' => 'post',
        'post_title' => $post_title,
        'post_content' => 'Lorem ipsum'
            ));
}
0md85ypi

0md85ypi2#

我有同样的问题,并解决了它与头部和模具功能!
插入文章后,重定向到当前页面和模具。

$post_id = wp_insert_post($post_arg);
if (!is_wp_error($post_id)) {
    header("Location: https://example.com/current-dir/");
    die();
}

相关问题