您的位置:首页 > 其它

如何删除WP系统程序中标题重复的文章

2014-05-15 10:51 281 查看
在迁移Wordpress数据的过程中(或者采集过程中),可能会遇到这种问题:同样一篇文章被导入了2次或者3次,这时候就要删除重复的文章了。
SQL语句删除

在Mysql中执行:

CREATE TABLE temp_table AS SELECT MIN(ID) AS col1 FROM wp_posts GROUP BY post_title;
DELETE FROM wp_posts WHERE ID NOT IN (SELECT col1 FROM temp_table);
DROP TABLE temp_table;

如果数据库中的某些文章被重复了1次,也就是一篇文章有两个,这时候只需要执行一次上面的SQL语句就行了,如果转子泵数据库中一篇文章被重复了2次,也就是存在3篇同样的文章,那么,就需要执行两次上面的SQL语句。
使用PHP删除

其实也是执行SQL语句,不推荐,因为比较麻烦:要新建文件,上传文件,从浏览器中执行,然后再删除这个转子泵文件。
使用插件Delete Duplicate Posts

插件,就不多说了,下载,激活,使用。

在Wordpress中,如何根据给定的用户ID数组查询其评论。
自定义查询的方法

默认的查询

global $wpdb;
$comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE comment_approved = '1' Limit 0, 10" );

所以:

global $wpdb;
$user_id = 57;
$comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_approved = '1' AND user_id = '%d' Limit 0, 10", $user_id ) );

还可以:

global $wpdb;
$user_ids = array( 57, 105, 567, 2820 );
$comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_approved = '1' AND user_id IN(%s) Limit 0, 10", $user_ids ) );

还可以对其进行缓存以提升加载速度:

global $wpdb;
$user_ids = array( 57, 105, 567, 2820 );
$key = md5( serialize( $user_ids ) );
$comments = get_transient( $key );
if( false === $comments ) {
$comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_approved = '1' AND user_id IN(%s) Limit 0, 10", $user_ids ) );
set_transient( $key, $comments, 10800 ); // Cache for 3 hours

使用HOOK过滤的方法

add_filter( 'comments_clauses', 'pw_comments_by_users', 10, 2 );
$comments = get_comments();http://www.xinhuanet.com/
remove_filter( 'comments_clauses', 'pw_comments_by_users' );
function pw_comments_by_users( $clauses, $wp_comment_query ) {
$clauses['where'] .= ' AND user_id IN (57,105,567,2820)';
return $clauses;
}

结论

使用自定义查询并对查询进行缓存的方法性能较好,使用HOOK过滤的方法比较简单,但是注意一定要在查询后移除HOOK,否则会影响到其它地方对于评论的查询
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: