Stay Tuned!

Subscribe to our newsletter to get our newest articles instantly!

Programming

Quickly Fix Your WordPress Views After Changing Themes

Introduction

UPDATE: [2023-01-26] Fix the update query.

Welcome to our tutorial on quickly fixing your WordPress views after changing templates! If you’ve recently switched to a new WordPress theme and noticed that your views are not displaying correctly, you’re not alone. This is a common issue that can be caused by various factors, including changes to the theme’s code or conflicts with your existing content.

Fortunately, you can take a few simple steps to resolve this issue and get your views back on track. In this tutorial, we’ll walk you through updating your views to ensure they are correctly displayed on your WordPress site.

Before we begin, it’s important to note that this tutorial assumes you have some basic knowledge of WordPress and are comfortable working with the MySQL or MariaDB database. If you’re new to WordPress, MySQL, or MariaDB or need a refresher on these topics, you may want to start with some of the other tutorials before proceeding.

Let’s get started!

Solution

You need to back up your WordPress website manually (web and database) or via the plugin. We use a plugin called UpdraftPlus. We installed and did the backup.

Next step, we access the database either via the command line or the easiest one, open up the PHPMyAdmin website and run the query from there.

When we researched what database tables we needed to modify, we came across this valuable information from WPBeaches.com on removing custom fields meta keys on the wp_postmeta table.

From here, we started playing around with the queries.

We started with this query:

select * from wp_posts;

Then we pick a blog ID as an experiment, in this case: 10631

select * from wp_postmeta 
where meta_key like '%view%' AND post_id = 10631;

We found the meta_value:

zivah_post_views_count = 253
_benqu_views_count = 9
In our case, we have a new template called a Benqu and an old one called Zivah. We need to update the _benqu_views_count meta_value with zivah_post_views_count

Then we figure out to join these tables:

select a.post_id, a.meta_key, a.meta_value, b.meta_key, b.meta_value 
from wp_postmeta a 
join (select post_id, meta_key, meta_value from wp_postmeta WHERE meta_key = "zivah_post_views_count") b 
	on a.post_id = b.post_id
where a.meta_key = "_benqu_views_count"

You will see that the _benqu_views_count meta_value is a lower amount than the zivah_post_views_count meta_value.

The next move is to update these.

We tested the update targetting one blog post first and picked the post_id = 10631 as an example.

UPDATE wp_postmeta a
JOIN (SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE meta_key = "zivah_post_views_count") b 
	ON a.post_id = b.post_id
SET a.meta_value = a.meta_value + b.meta_value
WHERE a.meta_key = "_benqu_views_count" AND
		a.post_id = 10631;
Take note of these two values, as you need this at the final adjustment after the update query for all posts below (optional).

Rerun the join query:

select a.post_id, a.meta_key, a.meta_value, b.meta_key, b.meta_value 
from wp_postmeta a 
join (select post_id, meta_key, meta_value from wp_postmeta WHERE meta_key = "zivah_post_views_count") b 
	on a.post_id = b.post_id
where a.meta_key = "_benqu_views_count"
        and a.post_id = 10631;

You should see the meta_value changed as a sum of _benqu_views_count meta_value and zivah_post_views_count meta_value.

Run the final update query:

UPDATE wp_postmeta a
JOIN (SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE meta_key = "zivah_post_views_count") b 
	ON a.post_id = b.post_id
SET a.meta_value = a.meta_value + b.meta_value
WHERE a.meta_key = "_benqu_views_count";

Recheck the join query:

select a.post_id, a.meta_key, a.meta_value, b.meta_key, b.meta_value 
from wp_postmeta a 
join (select post_id, meta_key, meta_value from wp_postmeta WHERE meta_key = "zivah_post_views_count") b 
	on a.post_id = b.post_id
where a.meta_key = "_benqu_views_count";

Check the website by looking for each blog; you will see that it will reflect on the blog detail.

What next from here

We are totally in security. So once this project is completed, we disable the remote database connection on either the PHPMyAdmin website or the database firewall port and IP address.

Next, you can clean up the bloated meta post for that particular theme or template. You can follow this step-by-step from the WPBeaches.com blog.

Lastly, in our case, we use the Post Views Counter by Digital Factory side with the out-of-the-box page view as we didn’t know how to update this previously. Now, we know how to do this next time, so we disabled and deleted this plugin. We could technically run side by side, BUT then we need to figure out how to sync this view regularly. This will be the next blog post 🙂

The End

Congratulation on reaching the end of this article. We hope we have shed some light on outlining quickly you fix your WordPress views after changing themes.

Let’s Discuss – We would love to hear about how you deal with the situation and what problems you might be facing. Please feel free to comment below this article or jump into our Dewachat and let us know if you have any questions we can answer!

We write this so that this is not a fixed article. Like in this journey, we learn as we go and re-write some parts, so please keep pinging with this article.

Take a Look – To boost your product and service to the broader web community, you can visit our Dewalist classified website – home to 40,000+ active users and 51,000+ one year of active advertising so far.

If you like this post, please check out our other related posts:

Try it Out – To solve, maintain and monitor Search Engine Optimisation (SEO) and other web challenges, check out our Dewagear tools website – home to 50+ tools so far.

If you love this article or any programming posts and you would like to receive an update on this article or our latest post, please sign up for the form below:

Newsletter signup

This is a newsletter for tech, creative, gadgets, games and crypto.

Please wait...

Thank you for sign up!

Thank you.

Credits

Photo by Pixabay from Pexel

Avatar

Valdy

About Author

Valdy founded Dewacorp.com, a helpdesk and IT Services provider that has taken care of over 39,000 customer service requests in the past 15 years - that's nearly 2,600 per year. He also created the growing Dewalist.com - a classified website that has close to 55,000 page views and 6,000 user visits per month with 52,000 yearly published ads and 39,000 active registered users. He blogs for the ever-growing Dewapost.com, a tech blog that gets around 20,000 impressions per month with 700 clicks and 10 average pages first impressions on Google Search. You can reach him on the Contact Us page, social media links below or Dewachat.com.

You may also like

Programming

SQL: Performance Tuning Query

Queries — Basic setup DBCC DROPCLEANBUFFERSDBCC FREEPROCCACHE SET STATISTICS IO ONSET STATISTICS TIME ON /* SELECT * FROM dbo.Categories*/ Sources
Programming

Notes: Road to Yii Framework

Apache2 Configuration on MAC Snow Leopard: /etc/apache2/httpd.conf “It worksP page is located under: /Library/WebServer/Documents/index.html.en To restart apache2 after changing: sudo apachectl