Need a MySQL query to delete Wordpress comments - mysql

My wordpress site was recently hacked so I had to reinstall everything. Wordpress resinstalled, database backups imported, everything fine and dandy. Then I installed the Disqus plugin and synced (Disqus was previously used before the site was hacked). Big no-no apparantly. Now I have a duplicate of every single comment on my site! Even worse, the duplicate comments have been synced BACK to Disqus!
So, I know this is a simple query in PHPMyAdmin but I don't know it! Please help!

Keep in mind that there is a WordPress Stack Exchange website ;)
I would use PHP unless you know the wpdb name off the top of your head. Be sure to back up the DB first! Like so:
global $wpdb;
$comments = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."_comments"
." ORDER BY comment_post_ID, comment_content");
$prev = NULL;
foreach($comments as $comment) {
if ($prev && $prev->comment_content == $comment->comment_content
&& $prev->comment_post_ID == $comment->comment_post_ID ) { // add maybe other rules here
$wpdb->query("DELETE FROM ".$wpdb->prefix."_comments" WHERE comment_ID == $comment- >comment_ID");
}
else
$prev = $comment;
}

If he doesn't know how use mysql, I think re editing the wp interface is great, because usually wp creating there build function for queries. And it may lead again to destruction of it.
The only reason why it doesn't work maybe its because of the plug in they use.! I think so.!

The query for deleting in mysql is
Delete from (tablename) where (primarykey)
But I think wordpress has its build in database queries for select delete and update.
Here's another if you use the plugin Disqus
If you're logged into your Disqus account you can also choose to delete a comment at your dashboard. This will remove it from your profile and remove all identifying information from the comment on the original page. Once a comment has been anonymized it cannot be claimed again.

Related

How Do I Bulk Delete All Wordpress Posts in a Specific Category or Before a Specific Date?

I have some categories on a site with over 350,000 active posts some of which have as many as 50,000 posts. The volume makes using a bulk delete plugins not viable because they all timeout and delete nothing. The best feedback I've gotten from the Wordpress support forum is to use WP-CLI but beyond that all they've done is give me a link to the WP-CLI website which contains no example specific to my needs.
I have never used CLI before beyond a failed attempt to run Media Cleaner which resulted in a timeout at 999 seconds. That was months ago so I forget how I tried to do that. Could someone please post a code snippet that could basically be copied and pasted into CLI with the only change I need to make being swapping the category ID or date/ID number of the oldest post I want to keep?
Again, I want to be able to delete all posts from a specific category or before a specific date/ID.
Alternatively, could someone post a SQL statement so that I can do this using MySQL without installing CLI? The SQL would be better because I could apply it to any site without installing let alone learning CLI.
You don't have access to phpmyadmin?
You could avoid all of that just by deleting posts on load:
/**
* REMOVE EVIL POSTS IF AND WHEN LOADED
* add to functions.php
* works on regular posts and simple categories,
* check codex for details if custom post types, custom taxonomies, etc.
**/
add_action( 'template_redirect', 'delete_post_on_load' ) ;
function delete_post_on_load() {
global $post ;
//has term works more consistently if all variables are supplied
if ( has_term( array( 'Category to Go', 'Second Category to Go' ), 'category', $post->ID ) ) {
wp_delete_post( $post->ID, true ) ;
wp_safe_redirect( site_url() ) ; //redirect to home instead of 404 on deletion
exit() ;
}
}

CakePHP Error: SQLSTATE[42S02] table not found - but exist

You might read this question every day so i tried another Stackoverflow's answer before asking:
CakePHP table is missing even when it exists
Anyways. The table i try to select data from does exist (quadra-checked uppercase/lowercase!) and it gets also listed via $db->->listSources().
Here's a screenshot of the query, the message and the last result from listing all Datasource's tables:
http://i.stack.imgur.com/CdhcV.png
Note: If i run this query in PHPMyAdmin manually it works fine. I would say its impossible to get the pictures output at one time in a view - now its up to you to tell me the opposite. By the way: I am pretty sure to use the correct Datasource.
I should tell additionally that the mysql-server is hosted on another platform. Since i can use it for my localhost-phpmyadmin if i modify the config.inc.php i can promise it is no Firewall-Problem.
Written in behalf of xcy7e:
The mistake was to execute the Query from the local Model. Here's the code:
$conn = ConnectionManager::getDataSource('myDB');
$conn->query($query);
// instead of $this->query($query);

Codeignighter Record wont insert

Using CI for the first time and i'm smashing my head with this seemingly simple issue. My query wont insert the record.
In an attempt to debug a possible problem, the insert code has been simplified but i'm still getting no joy.
Essentially, i'm using;
$data = array('post_post' => $this->input->post('ask_question'));
$this->db->insert('posts', $data);
I'm getting no errors (although that possibly due to disabling them in config/database.php due to another CI related trauma :-$ )
Ive used
echo print $this->db->last_query();
to get the generated query, shown as below:
INSERT INTO `posts` (`post_post`) VALUES ('some text')
I have pasted this query into phpMyAdmin, it inserts no problem. Ive even tried using $this->db->query() to run the outputted query above 'manually' but again, the record will not insert.
The scheme of the DB table 'posts' is simply two columns, post_id & post_post.
Please, any pointers on whats going on here would be greatly appreciated...thanks
OK..Solved, after much a messing with CI.
Got it to work by setting persistant connection to false.
$db['default']['pconnect'] = FALSE;
sigh
Things generally look ok, everything you have said suggests that it should work. My first instinct would be to check that what you're inserting is compatible with your SQL field.
Just a cool CI feature; I'd suggest you take a look at the CI Database Transaction class. Transactions allow you to wrap your query/queries inside a transaction, which can be rolled back on failure, and can also make error handling easier:
$this->db->trans_start();
$this->db->query('INSERT INTO posts ...etc ');
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE)
{
// generate an error... or use the log_message() function to log your error
}
Alternatively, one thing you can do is put your Insert SQL statement into $this->db->query(your_query_here), instead of calling insert. There is a CI Query feature called Query Binding which will also auto-escape your passed data array.
Let me know how it goes, and hope this helps!

Combining multiple Wordpress database queries

Forgive me for re-wording and re-asking this question, but the answers I received a couple weeks back didn't help much...
Basically, I'm looking to somehow combine multiple database queries in Wordpress to retrieve user IDs by searching for term in the 'usermeta' table, but only entries that have a certain 'meta_value'
I'm trying to combine:
$users = $wpdb->get_results("SELECT user_id, meta_value as 'business_name'
FROM $wpdb->usermeta
WHERE meta_key = 'business_name'");
AND:
$users = $wpdb->get_results("SELECT user_id, meta_value as 'business_description'
FROM $wpdb->usermeta
WHERE meta_key = 'business_description'");
To essentially have this:
$users = $wpdb->get_results("SELECT user_id, business_name, business_description
FROM
WHERE
business_name LIKE '%{$keyword}%' OR
business_description LIKE '%{$keyword}%'");
I've looked into INNER JOINs and subqueries, but cannot seem to find a good solution. I realize that I can get away with multiple queries, but this would be searching through possibly thousands of entries, so I'd like to optimize it as much as possible.
Hi #John:
If I understand your question correct (since you gave a technical example of what you were attempting but not a description of what end result you were trying to accomplish I'm not sure) it seems you are doing a user search? If yes, below is what I think you need.
<?php
// Load WordPress, but only for standalone example use
include '../wp-load.php';
// Make sure the database object is available
global $wpdb;
// Get value entered by the user
$keyword = $_GET['keyword'];
// This would be used in your code; the percent would confuse prepare() below
$keyword = "%{$keyword}%";
// Join each meta field as a separate join and reference the meta_key in the join
// prepare() below replaces "%s" with a quoted string value
$sql =<<<SQL
SELECT
users.user_nicename AS user_name,
bizname.meta_value AS business_name,
bizdesc.meta_value AS business_description
FROM
{$wpdb->users} AS users
INNER JOIN {$wpdb->usermeta} AS bizname
ON bizname.user_id=users.ID
AND bizname.meta_key='business_name'
INNER JOIN {$wpdb->usermeta} AS bizdesc
ON bizdesc.user_id=users.ID
AND bizdesc.meta_key='business_description'
WHERE 1=0
OR bizname.meta_value LIKE %s
OR bizdesc.meta_value LIKE %s
SQL;
// User prepare() to avoid SQL injection hacks
$sql = $wpdb->prepare($sql,$keyword,$keyword);
// Finally, get yer results!
$users = $wpdb->get_results($sql);
echo "<ul>";
foreach($users as $user) {
echo "<li>User= {$user->user_name}, Business= {$user->business_name}:{$user->business_description}</li>";
}
echo "<ul>";
The above is a complete working example you can copy to a file in the root of your website and call it something like /test.php allowing you to see it work by using a URL like this:
http://example.com/test.php?keyword=Accounting
Of course, this may be less performant at times than using multiple queries because of the query caching systems built-in to WordPress but it's impossible to tell without some benchmarking.
Hope this helps.
-Mike
P.S. By the way, I'm assuming you were not aware of it but since your prior question evidently hadn't gotten much WordPress love nor had this one I'll mention the WordPress Answers website which is a sister site to StackOverflow. Lots of WordPress enthusiasts are on hand over there to answer WordPress-specific questions.My experience with StackOverflow is they have some of the best developers on the web but few here have specific experience developing with WordPress so you end up with people here trying to answer MySQL questions without knowing the WordPress database schema and without knowing WordPress-specific best practices. Ask over at WordPress Answers and I think you'll an improved quality of answers to your WordPress-specific questions.
Can you post some details on the error messages you are receiving? The SQL here looks ok, but it's impossible to tell what the problem is without seeing the error messages.
For example, are you sure the $keyword field is being populated? How many results to return? Have you tried it without the WHERE statement or just a single OR to troubleshoot? Also, have you tried running this SQL directly on your server with some test keywords?
Give us more of an idea on what you have tried to fix the problem and we'll be able to assist further.
Kind Regards
Luke Peterson

Delete Old or Redundant Post Metadata from Wordpress

It appears that somehow in the past, WordPress saved multiple redundant post_meta for post revisions which are no longer in the database.
As a result, I have a ton of post_meta that does nothing and is tied to posts that no longer exist.
Does anyone know how to remove this data from phpMyAdmin with a SQL query?
You can run a mysql query like this; WordPress › Support » SQL Query to delete orphans wp_postmeta, but might be easier and safer to use a plugin like WordPress › Mass Custom Fields Manager « WordPress Plugins or WordPress › Custom Field Taxonomies « WordPress Plugins
"Optimize Database" is the one I use. Setup is very quick and easy. It runs in the background daily cleaning out post-meta orphans, things you have trashed, and useless old "revisions".
This has been explained here in full.
Remove Unnecessary WP Postmeta
All you have to do is add the following in your theme functions.php file
function delete_useless_post_meta() {
global $wpdb;
$table = $wpdb->prefix.'postmeta';
$wpdb->delete ($table, array('meta_key' => '_edit_last'));
$wpdb->delete ($table, array('meta_key' => '_edit_lock'));
$wpdb->delete ($table, array('meta_key' => '_wp_old_slug')); }
add_action('wp_logout','delete_useless_post_meta');