How to Pin Product at Top of WooCommerce Shop
Want to make sure a specific product always appears at the top of your WooCommerce shop, no matter how customers sort the products?
Whether it’s a featured item, a best-seller, or a seasonal special, pinning a product to the top can grab attention and boost sales. This guide walks you through a simple code snippet to achieve this, explains how it works, and tells you what to expect when you use it. No advanced coding skills needed, just follow along.
add_action( 'woocommerce_product_query', function( $q ) {
$pinned_product_id = 123; // <-- Change 123 to your product ID
// Validate product ID and ensure it’s a published product
if ( ! is_numeric( $pinned_product_id ) || $pinned_product_id < 1 || ! get_post( $pinned_product_id ) || 'publish' !== get_post_status( $pinned_product_id ) ) {
return;
}
// Get current product IDs in the query
$posts_in = (array) $q->get( 'post__in' );
// Remove pinned product from the list to avoid duplicates
$posts_in = array_diff( $posts_in, [ $pinned_product_id ] );
// Prepend the pinned product to the list
array_unshift( $posts_in, $pinned_product_id );
// Set the modified post__in array
$q->set( 'post__in', $posts_in );
// Ensure the order respects post__in while preserving existing sorting
$current_orderby = $q->get( 'orderby' );
if ( ! is_array( $current_orderby ) ) {
$q->set( 'orderby', [ 'post__in' => 'ASC', $current_orderby => $q->get( 'order' ) ] );
} else {
$q->set( 'orderby', array_merge( [ 'post__in' => 'ASC' ], $current_orderby ) );
}
}, 999 );How It Works
This snippet is like giving your chosen product a VIP pass to the front of the line. Here’s a breakdown of what it does in plain English:
- Targets Your Product: You tell the code which product to pin by entering its ID (e.g., replace 123 with your product’s ID). You can find the ID in your WordPress admin by editing the product and looking at the URL (it’ll say something like post=123).
- Checks the ID: The code makes sure the ID is valid and that the product is published. This prevents glitches if you accidentally use a wrong or unpublished product ID.
- Moves It to the Top: The code takes the list of products that WooCommerce is about to show, removes your pinned product (to avoid duplicates), and places it at the very top.
- Keeps Sorting Intact: Whether customers sort by price, popularity, or date, your pinned product stays first, and the rest of the products follow the chosen sorting order.
- Works Everywhere: This code applies to all WooCommerce product pages—your main shop page, category pages, tag pages, and even search results.
What to Expect
Once you add this code, here’s what you’ll see:
- Pinned Product Stays First: Your chosen product will always appear as the first item in your shop, no matter how customers sort the products (e.g., price low to high, newest first).
- Works Across Shop Pages: Whether customers are browsing the main shop, a category, or searching for products, your pinned product will be front and center.
- No Duplicates: The code ensures your pinned product doesn’t show up twice in the list.
- Smooth Sorting for Other Products: The rest of your products will still sort as expected (e.g., by price or popularity) after the pinned product.
How to Use It
- Find Your Product ID: Go to your WordPress admin, navigate to Products, edit the product you want to pin, and note the ID from the URL (e.g., post=123).
- Add the Code: Copy the snippet above, replace 123 with your product ID, and paste it into your theme’s functions.php file or a custom plugin. If you’re not sure how, use a plugin like Code Snippets to add it safely.
- Clear Cache: If you use a caching plugin (like WP Rocket), clear your site’s cache after adding the code.
- Test It Out: Visit your shop, category pages, or try a search. Sort the products in different ways to confirm your pinned product stays at the top.
Troubleshooting Tips
- Product Not Showing First? Double-check the product ID and ensure the product is published. Also, check for other plugins or theme code that might interfere with product sorting.
- Using a Custom Shop Layout? If your theme uses a custom product loop (e.g., via shortcodes), this code might not work. You may need to adjust it for your specific setup—reach out to a developer if needed.
- Blog Posts? This code only works for WooCommerce products, not blog posts. If you want to pin a blog post, you’d need a different snippet targeting WordPress’s main query.
Why This Helps Your Shop
Pinning a product to the top is a great way to highlight a special item, maybe a limited-time offer or your best-selling product. It ensures customers see it first, increasing the chances they’ll click and buy. Plus, this snippet is lightweight, doesn’t require a plugin, and works with most WooCommerce setups.
Give it a try, and watch your featured product shine at the top of your shop.
Written by Milan on 4th July 2025

