Dotlayer
  • News
  • Startups
  • Tutorials
  • SEO
  • Marketing
  • Interviews
No Result
View All Result
Dotlayer
  • News
  • Startups
  • Tutorials
  • SEO
  • Marketing
  • Interviews
No Result
View All Result
Dotlayer
No Result
View All Result

14 Handy WordPress MySQL/MariaDB Query Snippets and Hacks

August 24, 2017
in Tutorials
0 0
Share on FacebookShare on Twitter

In today’s post, we will be going over a few MySQL queries that allow you to make changes to your database. We recommend that before making any changes to your WordPress database, you perform a full backup and do not run these queries on a production website.

Additionally, these should only be run by developers or engineers who are already familiar with the WordPress database and framework. If you need help, feel free to contact our premium support for 24/7 WordPress support and development.

Let’s proceed to see the useful WordPress database snippets below:

When you need to change the site URL as well as the home URL of your website, you can use the snippet below:

UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldsiteurl.com', 'http://www.newsiteurl.com') WHERE option_name = 'home' OR option_name = 'siteurl';

When you need to change the GUID of your WordPress website

UPDATE wp_posts SET guid = REPLACE (guid, 'http://www.oldsiteurl.com', 'http://www.newsiteurl.com');

You can use the snippet below when you need to change the URLs in your post content

UPDATE wp_posts SET post_content = REPLACE (post_content, 'http://www.oldsiteurl.com', 'http://www.newsiteurl.com');

The snippet below helps you change the Image Path in your posts content

UPDATE wp_posts SET post_content = REPLACE (post_content, 'src="http://www.oldsiteurl.com', 'src="http://yourcdn.newsiteurl.com');
UPDATE wp_posts SET  guid = REPLACE (guid, 'http://www.oldsiteurl.com', 'http://yourcdn.newsiteurl.com') WHERE post_type = 'attachment';

When you need to update all your post meta

UPDATE wp_postmeta SET meta_value = REPLACE (meta_value, 'http://www.oldsiteurl.com','http://www.newsiteurl.com');

You can use this snippet to change the username of the admin user to something else

UPDATE wp_users SET user_login = 'Your New Username' WHERE user_login = 'Admin';

This is particularly useful when you need to reset your password in the database. This is particularly useful when you get logout of the website

UPDATE wp_users SET user_pass = MD5( 'new_password' ) WHERE user_login = 'your-username';

When you need to reassign articles between authors. The example below is assign all articles by Author B to Author A

UPDATE wp_posts SET post_author = 'new-author-id' WHERE post_author = 'old-author-id';

This query becomes useful when you want to delete all post revisions. Post revisions tend to grow and increase the size of the database, this snippet usually comes in handy.

DELETE a,b,c FROM wp_posts a LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id) LEFT JOIN wp_postmeta c ON (a.ID = c.post_id) WHERE a.post_type = 'revision'

When you want to delete all meta_keys for a post. Usually, plugins and function tend to use this to store their configuration, once the plugin is uninstalled it can be helpful to remove all the metadata they stored with it.

DELETE FROM wp_postmeta WHERE meta_key = 'your-meta-key';

To view all comment emails with no duplicates, use the snippet below:

SELECT DISTINCT comment_author_email FROM wp_comments;

To delete all pingbacks, use the snippet below:

DELETE FROM wp_comments WHERE comment_type = 'pingback';

This snippet below allows you to delete all spam comments

DELETE FROM wp_comments WHERE comment_approved = 'spam';
#     * 0 = Comment Awaiting Moderation
#     * 1 = Approved Comment
#     * spam = Comment marked as Spam

To view all tags that are not used, you can use the following MySQL query. You can view and then proceed to delete all tags that are not used.

SELECT * From wp_terms wt INNER JOIN wp_term_taxonomy wtt ON wt.term_id=wtt.term_id WHERE wtt.taxonomy='post_tag' AND wtt.count=0;
ShareTweetPin
Previous Post

VestaCP; Installing the Free Website Management Utility on Ubuntu

Next Post

How to Use WP_Query to Display the Most Recent Posts in Your WordPress Theme

Next Post

How to Use WP_Query to Display the Most Recent Posts in Your Wordpress Theme

You might also like

Calendarific Unveils New Pricing Plans

Calendarific Unveils New Pricing Plans

July 27, 2023
CurrencyBeacon vs. Currency Freaks, Fixer.io, and OpenExchangeRates: Which API is Best?

CurrencyBeacon vs. Currency Freaks, Fixer.io, and OpenExchangeRates: Which API is Best?

June 17, 2023
Mint Linux vs Ubuntu: Which is Right For You?

Mint Linux vs Ubuntu: Which is Right For You?

March 12, 2022
Net Neutrality: What is it and Why Should You Care?

Net Neutrality: What is it and Why Should You Care?

March 12, 2022
Solid State Drives – Why You Should Buy One Today

Solid State Drives – Why You Should Buy One Today

March 12, 2022

Machine Learning Algorithms Every Beginner Should Know

January 25, 2022
  • Terms of Service
  • Privacy Policy
  • Careers

© 2021 Dotlayer.com

No Result
View All Result
  • About Us
  • Advertise
  • Blog
  • Careers
  • Contact
  • Contact Us
  • Get Featured
  • Home Layout 1
  • Home Layout 2
  • Home Layout 3
  • Privacy Policy
  • Security
  • Services
  • Subscribe To Dotlayer
  • Terms of Service
  • Write For Us

© 2021 Dotlayer.com

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In