Solved

wordpress - sql query - comments table - delete all comments shorter that 50 characters

Posted on 2014-01-29
3
176 Views
Last Modified: 2014-09-19
On wordpress 3.8

on comments table

What sql query will delete all comments shorter than 50 characters  



P.S.

I have 10 questions, highest point level - open about sql queries

Soo, Lots of points for an sql guy
0
Comment
Question by:Matt_Walker01
3 Comments
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
ID: 39820644
Something like this would do it.
$comments = $wpdb->get_results("SELECT * FROM wp_comments WHERE comment_approved=1");
foreach ($comments as $comment) {
    if (strlen($comment->content) < 50) {
        $wpdb->delete('wp-posts',
		array('comment_ID' => $comment->comment_ID)
	);
    }
}

Open in new window

If you are wondering why your questions are being neglected I can offer a possibility. While there are some experts who collect points for sport on EE, most are genuinely interested in helping people with real world programming problems. The points are just recognition of their assistance. Your ten questions tend come off as a goof rather than real world problems. Why, for example, would anyone want to delete any comment shorter than 50 characters? Or why would anyone want to replace certain words in comments with random words? That's just messing with the data. It almost sounds malicious, like you're developing some malware to mangle WordPress sites.
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 39820926
Use this:
delete from comments_table where length(comments_field)<50

Open in new window

0
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 39820976
@Zberteoc

you have to be careful with the length function in MySQL because depending on the character type a character may count more than 1 byte.

length() function measures in bytes
char_length() function measures in characters.

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_length
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_char-length
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now