WooCommerce Expertise

Finding WooCommerce Products in the Database

Finding WooCommerce Products in the Database

Most of the time, the built-in features and functionalities of WooCommerce are more than sufficient for handling your products effectively. The WooCommerce user interface should be your go-to when editing and managing your products, but there may be certain situations where you find yourself needing to delve into your product database.

However, if you’re not an experienced developer, delving into your database on your own is not a good idea – you might crash your website by accident!

That’s where Saucal, a leading platform that’ll provide you with enterprise-level WooCommerce services from certified WooExperts, comes in. Our team of highly skilled professionals knows the ins and outs of the WooCommerce product database and will safely maintain and optimize it for efficiency.

Nevertheless, it’s still good to know when and why you might want to get hands-on with your database. In this article, we’ll tell you all about retrieving, searching, filtering, and optimizing your product data in your WooCoomerce database and why it can be risky to do so yourself. 

What is the WooCommerce product database?

Your database is a vital cog in your WooCommerce-powered machine. It serves as the central hub for storing all your product-related nuggets and information, like your product titles, descriptions, prices, and images. 

It’s the backstage conductor that ensures your online store runs smoothly and grants your customers a seamless shopping experience, especially as your business scales up.

When you first set up your WooCommerce plugin, WordPress automatically creates a few tables in its database dedicated to your WooCommerce information (API keys, attribute taxonomies, order and payment details, etc.).

The tables that get automatically generated by WordPress when the WooCommerce plugin is installed.

However, to be precise, your WooCommerce product data will be found in the wp_posts and wp_postmeta tables of your WordPress database. 

It’s crucial to note that the prefix of your tables might be different than “wp_”. In some cases, if you’ve used your host’s WordPress installer, your tables’ prefix will most likely be randomized to elevate your database security.

In order to access these two tables, or any other table in your WP database, you’ll need to go to your phpMyAdmin via the control panel of your hosting provider.

phpMyAdmin is a web-based database management system that comes pre-installed with most WordPress hosting providers. It has an easy-to-use interface that allows you to type SQL queries to manipulate your database using MySQL. 

On the left-hand side of your phpMyAdmin dashboard, you’ll be able to spot the name of your database (usually containing the name of your website), which you can then click on to expand and view all the tables.

The WordPress wp_posts and wp_postmeta database tables that contain the WooCommerce products.

When you add a new product to your online store, it gets saved in your database as a WordPress post with the post type of Product

The wp-posts table on phpMyAdmin showing the post_type of the WooCommerce products in the database.

The wp_posts table will hold product data, such as product titles, descriptions, and publication statuses. The wp_postmeta table, on the other hand, stores additional product details and metadata associated with your products in the wp_posts table. 

These two tables are linked to each other and can access each other’s data through the post_id field, which serves as a unique identifier for each product.

The ID attribute that links the wp_posts and wp_postmeta tables together.

Fair warning, tinkering about your database settings, configurations, or even table indexes when you don’t really need to or know what you’re doing can be a very risky game! 

Why would you want to find WooCommerce products in the database?

It’s important to stress that it’s highly unlikely for a typical store owner to find themselves in a situation where they need to directly access their database. 

The WooCommerce user interface (UI) is designed to handle all essential operations related to products, orders, inventory management, and more. 

However, there are a few exceptional scenarios where delving into the database becomes necessary, including:

  • Complex data export: If you require a comprehensive data export for advanced analysis or integration with external systems, accessing the database directly can provide you with the flexibility and control to obtain the precise information you need.
  • Bulk editing beyond UI capabilities: Although the UI offers extensive product management features, there may be instances where you need to perform bulk edits on product data that surpass the capabilities of the UI. In such cases, accessing the database allows you to make specific changes efficiently.
  • Database clean-up operations: Over time, your database can accumulate unnecessary data, such as old revisions, orphaned metadata, and expired transients, which can potentially impact its size and performance. By accessing the database, you can perform clean-up operations to remove redundant or obsolete data, optimizing your database’s efficiency.

Should you access the product database table by yourself?

If you’re not a tech wizard, it’s best to steer clear of trifling with your database settings, configurations, and table indexes on your own.

As a store owner, it’s generally recommended to rely on the WooCommerce UI for your day-to-day product management tasks. Instead of diving into the deep end, it’s wise to make the most of the built-in tools and features readily available. 

Of course, we understand you might find yourself in a pinch where you need to get your hands on your product database, like the scenarios we mentioned in the previous section. In such cases, it’s highly recommended to call upon the expertise of qualified developers – they’ll swoop in like superheroes and ensure that any database-related tasks are handled with finesse, efficiency, and a whole lot of skill.

Our crème de la crème of certified WooExperts are armed with invaluable insights and technical prowess needed to ensure accurate and efficient execution of database-related operations to improve your online store’s overall speed and performance.

How to find products in the WooCommerce database

If we haven’t emphasized it enough, we must make it abundantly clear: tampering with your database single-handedly can lead to data disasters if you’re not sure what you’re doing.

That being said, if you yearn to catch a glimpse of the backend intricacies of product database management and editing, we present you with this overview of what it takes.

1. Retrieving products from the database

If you’re interested in retrieving your products from your WooCommerce database in order to view or modify them, then you need to use SQL (Structured Query Language) queries.

You’ll need to do the following first to be able to run SQL queries: 

  1. Go to your phpMyAdmin dashboard via your hosting provider’s control panel.
  2. On the left-hand side, click on the name of your WordPress site to select your database.
  3. Click on the SQL tab from the menu at the top.
phpMyAdmin SQL query window.
  1. In the empty SQL query window, write a query to fetch the product information that you want from the wp_posts table. Let’s dive into a few examples here:

1.1 View all your published products 

For this first example, let’s say that you want to fetch all your published products from your WooCommerce database. You’re going to use the following query to do so:

SELECT p.ID, p.post_type, p.post_title, p.post_content, p.post_excerpt, p.post_status
FROM wp_posts p
WHERE p.post_type = 'product' AND p.post_status = 'publish';
Executing a SELECT SQL query.

Before executing this query, it’s vital that you change wp_posts with the name of the posts table in your database (since it’s most likely different for every WP website).

The SELECT keyword specifies the columns that you want to retrieve from your wp-posts table. post_type, post_title, post_content, post_excerpt, and post_status are the columns that we are going to be fetching the results for using this query. 

The FROM keyword specifies the table from which you want to retrieve your product data, which is the table wp_posts. The p after the table name is just an alias that you can assign to your table for convenience.

The WHERE keyword filters the rows of your table based on the specific conditions you give it. In this case, our conditions were that the values in the p.post_type = ‘product’ and the values in the p.post_status = ‘publish’.

Your output will look something like this:

The output of the first SQL query.

1.2 Retrieve additional product details from the wp_postmeta table

If you’re looking to fetch additional data relating to your WooCommerce products, such as their price and SKU, you have to use the JOIN clause to link your wp_posts and wp_postmeta tables to each other and display the information you need. Use the following sample SQL query, changing the name of the tables to match the posts and postmeta tables in your site’s database:

SELECT p.ID, p.post_type, p.post_status, p.post_title, pm1.meta_value AS price, pm2.meta_value AS sku
FROM wp_posts p
JOIN wp_postmeta pm1 ON p.ID = pm1.post_id AND pm1.meta_key = '_price'
JOIN wp_postmeta pm2 ON p.ID = pm2.post_id AND pm2.meta_key = '_sku'
WHERE p.post_type = 'product' AND p.post_status = 'publish';

The JOIN wp_postmeta pm1 ON p.ID = pm1.post_id AND pm1.meta_key = ‘_price’ is used here to connect the wp_posts table (given an alias of p) with the wp_postmeta table (given an alias of pm1). It will link your database product records based on the ID column from wp_posts and the post_id column from wp_postmeta. 

Additionally, it adds the condition where pm1.meta_key = ‘_price’ to only include post meta entries with the key _price.

We used a second JOIN statement in the above query, which is similar to the previous one, but this time it connects the wp_posts table (p) with the wp_postmeta table given a different alias of pm2. Again, the pm2.meta_key = ‘_sku’ condition is used to ensure that only the post meta entries with the key _sku are going to get displayed.

Using these two JOINS will help you match the relevant meta information (price and SKU) to each product entry based on their respective post IDs and meta key conditions in your WordPress database.

Your output should look something like this:

The output of the JOIN SQL query.

It’s super important to keep an eye out for any potential challenges or issues that might pop up along the way. Things like wonky query syntax or missing data can really throw a wrench in progress. 

2. Searching and filtering products

Searching and filtering products in your database will allow you to find specific products based on whatever criteria you want, such as product title, category, or price. You can do that by modifying your SQL queries to include specific conditions.

For example, to search for products with a specific title, you can use the following SQL query:

SELECT p.ID, p.post_title
FROM wp_posts p
WHERE p.post_type = 'product' AND p.post_status = 'publish' AND p.post_title LIKE '%Product Title%';

Don’t forget to change the posts table name to the specific name in your database to ensure that the query will be executed successfully.

The LIKE keyword is used for pattern matching. Here, we are specifying that the post_title column in the wp_posts table must contain the string ‘Product Title’ anywhere within it. 

The % symbols are wildcards, which means that any characters can appear before or after the ‘Product Title’ string. Just change the ‘Product Title’ to whatever criteria you want, and you can also just include the % symbols just before your string or just after your string, depending on the results you want – it doesn’t have to be placed on both ends of your string.

Your output should look similar to this:

he output of the searching and filtering SQL query using LIKE.

If you want to filter your products by category, you can use the JOIN clause to incorporate the wp_term_relationships and wp_terms tables in your query. 

These two tables are primarily related to the taxonomies and categorization of your WordPress content, containing information about your products’ categories and tags.

Here is a sample SQL for this:

LECT p.ID, p.post_title, t.name AS category
FROM wp_posts p
JOIN wp_term_relationships tr ON p.ID = tr.object_id
JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'product_cat'
JOIN wp_terms t ON tt.term_id = t.term_id
WHERE p.post_type = 'product' AND p.post_status = 'publish' AND t.name = 'Category Name';

The same concepts from earlier apply here, as well. Just change the ‘Category Name’ string to whatever you want to get your desired results and the table names to the specific names in your database. In our example, we replaced ‘Category Name’ with ‘Cosmetics’.

Your output should be similar to the following:

The output of the searching and filtering SQL query using the JOIN clause.

You can also do other filtering options, such as sorting by date, price, or popularity. For example, to sort products by price in descending order, you can try the following sample SQL query:

SELECT p.ID, p.post_title, pm.meta_value AS price
FROM wp_posts p
JOIN wp_postmeta pm ON p.ID = pm.post_id AND pm.meta_key = '_price'
WHERE p.post_type = 'product' AND p.post_status = 'publish'
ORDER BY price DESC;

The ORDER BY clause specifies the attribute (i.e., price) you’re going to be displaying your results according to. The DESC clause instructs your database to order your price attribute from biggest to smallest.

Your output should be similar to this:

The output of the first searching and filtering SQL query using the ORDER BY and DESC clauses.

When searching and filtering products in your database, you might stumble upon pesky challenges like complex query structures, slow query performance, or inaccurate results. 

Our custom WooCommerce solutions can help you optimize your product search and filtering efforts, ensuring a better user experience for both you and your customers.

3. Limiting results and viewing product information

Limiting the number of results returned by an SQL query is a handy approach to know because it can help you optimize your database’s performance. To do this, you can use the LIMIT clause in your SQL query. 

For example, to fetch the top 3 most expensive products, you can use the following query:

SELECT p.ID, p.post_title, pm.meta_value AS price
FROM wp_posts p
JOIN wp_postmeta pm ON p.ID = pm.post_id AND pm.meta_key = '_price'
WHERE p.post_type = 'product' AND p.post_status = 'publish'
ORDER BY price DESC
LIMIT 3;

Your results will appear like so:

The output of the first searching and filtering SQL query using the LIMIT clause.

Remember to always adjust the previous SQL query examples before you execute them with the parameters that match your specific database table names to get the desired outcome.

If you’re still running into problems like incomplete or inaccurate data, complex query structures, or slow query performance, our support and maintenance plans will help your online store remain operational and quickly resolve any database issues.

Get a free consultation with Saucal

Saucal’s free consultation web page.

Managing and optimizing your WooCommerce product database is essential for better store performance and improving your online store’s user experience. Consequently, you will need to have a solid understanding of your WooCommerce database structure and SQL queries. 

Don’t start meddling with your database unless you’re a professional, though – you’ll put your online store at risk! 

Instead, work with our team of certified WooExperts and utilize our custom WooCommerce solutions to solve all your issues relating to your product data management, database optimization, and overall store performance.

Our adroit professionals utilize a range of optimization techniques to enhance your store’s performance, like:

  • Database indexing: This enables faster product search and retrieval.
  • Query optimization: Our experts can optimize SQL queries to reduce the load on the database and improve the overall performance of your store.
  • Database cleanup: Removing unnecessary data, such as old revisions, orphaned metadata, and expired transients, is essential to streamline your database efficiency.
  • Server optimization: Our experienced developers can optimize your server environment by configuring caching mechanisms and fine-tuning server settings to enhance performance.

Ready to discover how our custom WooCommerce solutions, expert developers, and support and maintenance plans can improve your online store’s performance and user experience? Contact us to tell us all your specific needs for your WooCommerce store and its product database, and we’ll get in touch.

Start your journey with Saucal!