如何避免在wordpress中插入重复

时间:2021-12-09 04:19:20

I am trying to insert record into wordpress tables but the problem is, its inserting duplicate records.

我试图将记录插入到wordpress表中,但问题是,它插入了重复的记录。

This is my sql query

这是我的sql查询。

$qv = $conn->query("INSERT INTO wp_posts (post_title, post_content, post_name, post_date, post_date_gmt, post_author) VALUES('$title[$i]', '$description[$i]', '$url[$i]', '$date[$i]', '$postdate[$i]', '$author[$i]') ON DUPLICATE KEY UPDATE post_name = post_name");

I don't want to insert any duplicate records, how to fix this?

我不想插入任何重复的记录,如何修复?

3 个解决方案

#1


1  

create unique key on column and use insert ignore instead of insert like below

在列上创建唯一键,使用insert ignore代替insert

  ("INSERT ignore INTO wp_posts (post_title, post_content, post_name, post_date, post_date_gmt, post_author)
 VALUES('$title[$i]', '$description[$i]', '$url[$i]', '$date[$i]', '$postdate[$i]', '$author[$i]'));

#2


1  

wp_posts has id as auto increment primary key and your insert query does not have id, hence your on duplicate constraint will not work. If you want to have unique record by post title then you will have to create unique index on it. Unique constraint can be applied to combination of more than one column, if necessary. Also insert ignore will igonre the duplicate records and not update it. You will have to handle it in your application.

wp_posts有id作为自动递增主键,您的insert查询没有id,因此您的on duplicate约束将无法工作。如果您想要按post标题拥有唯一记录,那么您必须在其上创建惟一索引。如果需要,唯一约束可应用于多个列的组合。此外,插入“忽略”将触发重复的记录,而不是更新它。您必须在应用程序中处理它。

Query to add Unique Constraint in MySQL

查询在MySQL中添加唯一约束

ALTER TABLE wp_posts ADD CONSTRAINT unique_post UNIQUE (post_title,post_name);

#3


0  

Disallow Duplicate Post Using Titles

禁止重复使用标题

function disallow_posts_with_same_title($messages) {
    global $post;
    global $wpdb ;
    $title = $post->post_title;
    $post_id = $post->ID ;
    $wtitlequery = "SELECT post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND post_title = '{$title}' AND ID != {$post_id} " ;

    $wresults = $wpdb->get_results( $wtitlequery) ;

    if ( $wresults ) {
        $error_message = 'This title is already used. Please choose another';
        add_settings_error('post_has_links', '', $error_message, 'error');
        settings_errors( 'post_has_links' );
        $post->post_status = 'draft';
        wp_update_post($post);
        return;
    }
    return $messages;

}
add_action('post_updated_messages', 'disallow_posts_with_same_title');

#1


1  

create unique key on column and use insert ignore instead of insert like below

在列上创建唯一键,使用insert ignore代替insert

  ("INSERT ignore INTO wp_posts (post_title, post_content, post_name, post_date, post_date_gmt, post_author)
 VALUES('$title[$i]', '$description[$i]', '$url[$i]', '$date[$i]', '$postdate[$i]', '$author[$i]'));

#2


1  

wp_posts has id as auto increment primary key and your insert query does not have id, hence your on duplicate constraint will not work. If you want to have unique record by post title then you will have to create unique index on it. Unique constraint can be applied to combination of more than one column, if necessary. Also insert ignore will igonre the duplicate records and not update it. You will have to handle it in your application.

wp_posts有id作为自动递增主键,您的insert查询没有id,因此您的on duplicate约束将无法工作。如果您想要按post标题拥有唯一记录,那么您必须在其上创建惟一索引。如果需要,唯一约束可应用于多个列的组合。此外,插入“忽略”将触发重复的记录,而不是更新它。您必须在应用程序中处理它。

Query to add Unique Constraint in MySQL

查询在MySQL中添加唯一约束

ALTER TABLE wp_posts ADD CONSTRAINT unique_post UNIQUE (post_title,post_name);

#3


0  

Disallow Duplicate Post Using Titles

禁止重复使用标题

function disallow_posts_with_same_title($messages) {
    global $post;
    global $wpdb ;
    $title = $post->post_title;
    $post_id = $post->ID ;
    $wtitlequery = "SELECT post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND post_title = '{$title}' AND ID != {$post_id} " ;

    $wresults = $wpdb->get_results( $wtitlequery) ;

    if ( $wresults ) {
        $error_message = 'This title is already used. Please choose another';
        add_settings_error('post_has_links', '', $error_message, 'error');
        settings_errors( 'post_has_links' );
        $post->post_status = 'draft';
        wp_update_post($post);
        return;
    }
    return $messages;

}
add_action('post_updated_messages', 'disallow_posts_with_same_title');