WooCommerce: How to Remove Pages from Your Category Pages (SEO-Friendly Guide)
Introduction:
WooCommerce is a powerful and flexible e-commerce platform, but sometimes its default behavior doesn’t perfectly align with your specific business needs. One common customization request is removing pages from category pages. While WooCommerce automatically displays products within a category on its dedicated page, it doesn’t natively filter out standard WordPress pages. This can lead to a cluttered and confusing browsing experience for your customers. This article will guide you through how to remove pages Explore this article on How To Change Shop Page Url In Woocommerce from WooCommerce category pages and create a cleaner, more focused experience for your visitors, ultimately boosting conversions and improving your store’s SEO. We’ll cover both code-based solutions and plugin options.
Main Part:
The presence of unrelated pages on category pages can dilute the focus on your products, negatively impacting user experience and potentially harming your SEO. Imagine a customer searching for “red t-shirts” and being presented with an “About Us” page alongside the actual product listings. This dilutes the relevance of the category page and can frustrate the user. Therefore, removing these extraneous pages is crucial.
Why Remove Pages from Category Pages?
- Improved User Experience: A cleaner category page allows customers to find what they’re looking for faster, leading to higher engagement and reduced bounce rates.
- Enhanced SEO: Focused category pages signal relevance to search engines, improving your chances of ranking higher for target keywords.
- Increased Conversions: By presenting only relevant products, you minimize distractions and guide customers towards making a purchase.
- Professional Appearance: A well-organized store instills trust and credibility.
Method 1: Using Code (Recommended for Developers)
This method involves adding a snippet of PHP code to your theme’s `functions.php` file (or preferably a child theme’s `functions.php` file to prevent loss of customizations during theme updates). This code will filter the query on category pages to exclude pages.
Important: Always back up your website before making any code changes. Errors in your `functions.php` file can break your website.
1. Access Your `functions.php` File: Log in to your WordPress dashboard, go to Appearance -> Theme File Editor, and locate the `functions.php` file in your current (child) theme.
2. Add the Following Code Snippet:
/**
3. Explanation of the Code:
- `custom_exclude_pages_from_category( $query )`: This function takes the main query object as an argument.
- `! is_admin()`: Ensures the code only runs on the front-end of the website, not in the admin panel.
- `$query->is_main_query()`: Verifies that we are modifying the main query.
- `is_product_category()`: Checks if the current page is a WooCommerce product category page.
- `$query->set( ‘post_type’, ‘product’ )`: This is the key line. It modifies the query to only fetch posts of the `product` post type, effectively excluding pages.
- `add_action( ‘pre_get_posts’, ‘custom_exclude_pages_from_category’ )`: This hook tells WordPress to run our function before the query is executed.
Read more about How To Add Calculate Shipping In Woocommerce
4. Save the File: Click the “Update File” button to save your changes.
5. Test Your Category Pages: Visit your WooCommerce category pages to verify that the pages have been removed.
Method 2: Using a Plugin
If you’re not comfortable with code or prefer a more user-friendly solution, several plugins can help you achieve this. While there isn’t one plugin specifically designed *only* for removing pages from category pages, plugins that provide advanced product filtering or category customization often include this functionality.
Example: Custom Product Tabs for WooCommerce:
This plugin, while primarily designed for adding custom tabs to product pages, sometimes offers features to customize category pages. Check the specific features of the plugin to ensure it supports filtering by post type on category archives.
Benefits of Using a Plugin:
- Easy to Use: Plugins typically provide a user-friendly interface with options to configure the exclusion.
- No Code Required: No need to edit your `functions.php` file.
- Additional Features: Plugins often offer other valuable features for managing your WooCommerce store.
Downsides of Using a Plugin:
- Plugin Bloat: Too many plugins can slow down your website. Choose plugins carefully and only install what you need.
- Compatibility Issues: Plugins may conflict with other plugins or your theme.
- Potential for Updates: Plugins require regular updates to maintain compatibility and security.
Method 3: Using a combination of filters and conditional tags.
This is an advanced example that gives you even more flexibility:
/**
// Array of page IDs to exclude. Replace with your actual page IDs.
$excluded_page_ids = array( 123, 456, 789 );
// Build the meta query to only return products and exclude the specified pages.
$meta_query = array(
‘relation’ => ‘AND’,
array(
‘key’ => ‘_visibility’, //WooCommerce product visibility meta key
‘value’ => array( ‘visible’, ‘catalog’, ‘search’ ), //Show only visibile products
‘compare’ => ‘IN’
),
array(
‘key’ => ‘post_type’,
‘value’ => ‘product’,
‘compare’ => ‘=’,
),
array(
‘relation’ => ‘AND’,
array(
‘key’ => ‘ID’,
‘value’ => $excluded_page_ids,
‘compare’ => ‘NOT IN’
),
)
);
$query->set( ‘meta_query’, $meta_query );
$query->set( ‘post_type’, ‘product’ );
$query->set(‘suppress_filters’, false);
}
}
add_action( ‘pre_get_posts’, ‘custom_exclude_specific_pages_from_category’ );
Explanation of the code:
- `$excluded_page_ids = array( 123, 456, 789 );`: Replace these sample IDs with the actual IDs of the pages you wish to exclude. You can find the page ID in the URL when you edit the page in your WordPress admin. Example: `post=123` means the ID is `123`.
- This example sets the `post_type` to ‘product’.
- The meta query excludes the specific page IDs you put in `excluded_page_ids`.
- `$query->set(‘suppress_filters’, false);` ensures that other filters still work.
Important Considerations:
- Child Theme: Always use a child theme when modifying your theme’s files.
- Plugin Updates: Keep your plugins updated to ensure compatibility and security.
- Backup: Back up your website before making any changes.
- Testing: Thoroughly test your category pages after implementing any solution.
Conclusion:
Removing pages from WooCommerce category pages is a simple yet effective way to improve user experience, boost SEO, and increase conversions. Whether you choose the code-based approach or opt for a plugin, the key is to create a clean and focused browsing experience for your customers. By implementing one of the methods described in this article, you can ensure that your category pages are optimized for both search engines and your valuable customers, leading to a more successful online store. Remember to always back up your website before making any changes and thoroughly test your changes to ensure they work as expected.