如何删除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,否则会影响到其它地方对于评论的查询
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,否则会影响到其它地方对于评论的查询
相关文章推荐
- centOS 5 查看系统的版本和如何删除程序
- WORDPRESS MYSQL删除重复标题文章
- 如何干净删除安装的程序(debian系统)
- 织梦dedecms 采集的文章重复或内容文章标题重复批量删除的解决方法
- 如何从Android模拟器删除系统“无用”程序 获得root
- windows系统卸载程序不彻底,系统服务中有残留如何删除
- 好系统重装助手教你如何通过修改注册表完全删除程序
- WordPress删除数据中标题重复文章的方法
- 如何提高WP程序发布文章的质量
- centOS 5 查看系统的版本和如何删除程序
- 如何删除表中的重复记录?
- 如何防止同一帐户重复登录系统
- 如何防止控制客户端使其用同一帐户重复登录系统.
- 如何删除重复的数据?
- 如何替换Blog里文章等栏目标题变成相应的英文
- [老文章转载]将Java程序变成Windows系统服务
- 如何设置系统与正确删除软件和升级系统
- 程序如何删除自己
- 如何在客户端计算机上创建和删除隐藏共享或系统管理共享
- 如何查找、删除表中重复的记录