I recently noticed that our search results page is blank when a Part Number is searched that we do not have in stock. I would like to add some content when a search comes up empty. Currently, what I have coded in my custom search-results.php file is not working. Anybody have any ideas? The following is what I have in my search-results.php file:
<?php
/* Template Name: Search Results
*/
get_header();
?>
<section id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="page-title"><?php printf(__( 'Search Results for: %s', 'catalog-me' ), '<span>' . $_POST['term'] . '</span>' ); ?></h1>
</header><!-- .page-header -->
<?php
global $wpdb;
$PartNum = $_POST['term'];
$PartNum = sanitize_text_field( $_POST['term'] );
update_post_meta( $post->ID, 'term', $PartNum );
$query_Parts = $wpdb->get_results( "SELECT * FROM parts WHERE parts.PartNumber LIKE '%$PartNum%' or parts.nsn LIKE '%$PartNum%'");
//echo "<pre>"; print_r($query_Parts); echo "</pre>";
echo '<table class="results"><thead><tr><th>Part Number</th><th>NSN</th><th>Alternate Part Number</th><th>Description</th><th>Condition</th><th>Quantity</th></tr></thead><tbody>';
foreach($query_Parts as $row) {
echo '<tr>';
echo "<td>{$row->PartNumber}</td>";
echo "<td>{$row->nsn}</td>";
echo "<td>{$row->nsn2}</td>";
echo "<td>{$row->Description}</td>";
echo "<td>{$row->Condition}</td>";
echo "<td>{$row->CVQuantity}</td>";
echo '</tr>';
}
setup_postdata( $post );
echo '</tbody></table>';
?>
</h2>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, it doesn't look like we have that Part Number in our Inventory database. Try a new search with Alternate Part Number or NSN.</p>
<?php endif; ?>
</main><!-- #main -->
</section><!-- #primary -->
</div>
<?php
get_footer();
Any help would be greatly appreciated. Thank you!
]]>if ( have_posts() ) :
line is referencing the page that you’re on, not your query that you made below. I made some edits to your code to query the database, then check if that query’s results was an empty array. If not, show the results. If so, display your “Not Found” message.
Try this code:
<?php
/* Template Name: Search Results
*/
get_header();
?>
<section id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
global $wpdb;
$PartNum = $_POST['term'];
$PartNum = sanitize_text_field( $_POST['term'] );
update_post_meta( $post->ID, 'term', $PartNum );
$query_Parts = $wpdb->get_results( "SELECT * FROM parts WHERE parts.PartNumber LIKE '%$PartNum%' or parts.nsn LIKE '%$PartNum%'" );
if ( ! empty( $query_Parts ) ) : ?>
<header class="page-header">
<h1 class="page-title"><?php printf(__( 'Search Results for: %s', 'catalog-me' ), '<span>' . $_POST['term'] . '</span>' ); ?></h1>
</header><!-- .page-header -->
<?php
//echo "<pre>"; print_r($query_Parts); echo "</pre>";
echo '<table class="results"><thead><tr><th>Part Number</th><th>NSN</th><th>Alternate Part Number</th><th>Description</th><th>Condition</th><th>Quantity</th></tr></thead><tbody>';
foreach($query_Parts as $row) {
echo '<tr>';
echo "<td>{$row->PartNumber}</td>";
echo "<td>{$row->nsn}</td>";
echo "<td>{$row->nsn2}</td>";
echo "<td>{$row->Description}</td>";
echo "<td>{$row->Condition}</td>";
echo "<td>{$row->CVQuantity}</td>";
echo '</tr>';
}
echo '</tbody></table>';
?>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, it doesn't look like we have that Part Number in our Inventory database. Try a new search with Alternate Part Number or NSN.</p>
<?php endif; ?>
</main><!-- #main -->
</section><!-- #primary -->
</div>
<?php
get_footer();
]]>
I can’t thank you enough for your help. I made the changes you suggested and it worked perfectly!! I don’t mean to make it more difficult, but now that it works, would you be able to tell me how I can input the part number searched for that came up empty into the results? For example: “I’m sorry, it doesn’t look like we have P/N: (search term) in our Inventory database.”.
Again, thank you so much! You have saved me HOURS of time!
]]>You can add the search term by adding this bit of code where ever you want to display it on the page: <?php echo $PartNum; ?>
Glad I could help! ??
]]>Worked like a charm! Thank you so much.
You rock! ??
]]>