Lesson 27 Implementation — Adding Functional Search to the Flipnzee Auctions Table
Implementation Series: Building the Flipnzee Auctions Plugin Step by Step
Lesson: 27 (Implementation)
Prerequisites: Lessons 25 and 26 completed
Introduction
In Lesson 26, the Search Auctions box was added above the auctions table using WordPress’ built-in search_box() method. Although the interface looked complete, typing a keyword and clicking Search Auctions did not actually filter any records.
In this implementation lesson, the search box is connected to the database so administrators can quickly find auctions by Auction ID, Listing ID, or Status.
After completing this implementation, the search feature behaves much like the search functionality found on WordPress’ built-in Posts and Pages screens.
What Was Implemented
This lesson introduced the following improvements:
- Added support for search keywords
- Filtered database results using SQL
- Used secure SQL queries with
$wpdb->prepare() - Escaped wildcard characters using
$wpdb->esc_like() - Updated pagination so it counts only matching results
- Preserved the search keyword after searching
Step 1 — Update get_all_auctions()
Open:
includes/class-auction-manager.php
Locate the get_all_auctions() method.
Originally it accepted only pagination values:
public static function get_all_auctions(
$per_page = 20,
$offset = 0
)
Add a third parameter:
public static function get_all_auctions(
$per_page = 20,
$offset = 0,
$search = ''
)
This parameter will receive the administrator’s search keyword.
Step 2 — Modify the SQL Query
Previously, every auction was returned regardless of the search box.
The method was updated to check whether a search keyword exists.
If a keyword is present:
- Create a safe LIKE pattern using
$wpdb->esc_like() - Filter records using
WHERE - Return only matching auctions
Otherwise, return all auctions exactly as before.
This keeps the plugin efficient while remaining secure.
Step 3 — Read the Search Keyword
Open:
admin/class-auctions-table.php
Inside prepare_items(), retrieve the keyword submitted by WordPress.
The search box automatically sends its value using:
$_REQUEST['s']
The keyword was safely sanitized using:
wp_unslash()sanitize_text_field()
This protects the plugin from unsafe input.
Step 4 — Pass the Search Value
Previously, auctions were loaded like this:
Flipnzee_Auction_Manager::get_all_auctions(
$per_page,
$offset
);
The search variable was added as the third parameter:
Flipnzee_Auction_Manager::get_all_auctions(
$per_page,
$offset,
$search
);
Now the database query knows what the administrator searched for.
Step 5 — Update Pagination
Searching introduced one small problem.
Although only matching auctions appeared, pagination still counted every auction in the database.
To solve this:
The count_auctions() method was updated to accept the same search keyword.
Instead of counting every record:
SELECT COUNT(*)
the query now counts only matching auctions whenever a search is active.
This keeps pagination accurate.
Step 6 — Update the Table
The table’s total item count was changed from:
Flipnzee_Auction_Manager::count_auctions();
to:
Flipnzee_Auction_Manager::count_auctions(
$search
);
This small change synchronizes the search results with the page numbers.
Step 7 — Test the Feature
Several searches were performed during implementation.
Examples included:
- Auction ID
- Listing ID
- Auction Status
The table correctly displayed only the matching auctions.
Pagination also updated automatically.
For example:
Searching for:
24
displayed:
- 1 matching auction
- 1 item
- No additional pages
This confirmed that both searching and pagination were working together correctly.
Security Improvements
Several WordPress security functions were used throughout this lesson.
These included:
sanitize_text_field()wp_unslash()$wpdb->prepare()$wpdb->esc_like()
Using these functions helps protect the plugin against SQL injection and unsafe user input.
Troubleshooting
During implementation, one issue appeared.
The search results filtered correctly, but pagination still showed the total number of auctions.
The cause was simple.
The table was still calling:
count_auctions();
instead of:
count_auctions( $search );
After updating this single line, pagination immediately displayed the correct number of matching results.
Result
At the end of this lesson, the Flipnzee Auctions plugin now supports:
- ✅ Professional search box
- ✅ Secure database searching
- ✅ Pagination integrated with search
- ✅ Accurate item counts
- ✅ WordPress coding standards
The Auctions screen now behaves much more like WordPress’ own administration pages.
Conclusion
This implementation transformed the search box from a simple interface element into a fully functional administrative tool.
Combined with pagination from the previous lesson, administrators can now quickly locate auctions even when the database grows significantly.
The Flipnzee Auctions plugin continues to evolve from a learning project into a production-ready WordPress plugin.
Download Files
The source code for this lesson is available below:
- Starting Project: Download the plugin before implementing Lesson 27.
- Completed Project: Download the updated plugin after implementing the functional search feature and compare your changes if needed.
In the next implementation lesson, we’ll add sortable columns, allowing administrators to click table headers such as ID, Listing ID, Status, and Created Date to sort auction records just like the native WordPress admin screens.
