0

The website I am busy working on was built by another company that no longer supports Wordpress so I am looking for help. Below is the PHP for the search function. From the research Ive done it seems they have used the standard Wordpress code. The client has asked that the search only results in the related product. At the moment it is pulls everything with the key phase such as blog posts and titles.

<?php get_header(); ?>

    <div class="content pattern-background">
        <div class="wrapper">
          <h1>Search results:</h1>
          <div class="product-list">
           <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        <div class="product-list__item" >

                <?php
            if(has_post_thumbnail()) {
            $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
            } else {
            }
            ?>

              <a class="product__image" href="<?php the_permalink() ?>" style="background-image: url(<?php echo $url; ?>);">
              </a>
              <div class="product__details">
                <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
                <p><?php the_excerpt(); ?></p>
                <div class="divider-solid"></div>
              </div>
            </div>
      <?php endwhile; ?>


  <?php else : ?>

  <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
    <h2>Sorry, no items found.</h2>
  </div>

<?php endif; ?>
          </div>
        </div>


<?php get_footer(); ?>

Can someone please guide me on what code to add or remove? or if there is documentation that can help.

This is a very important client so I am nervous to just play around until I get it right.

Thanks in advance

2 Answers 2

0
This code will only search form woocommerce products. If you want to show any other search result from another post type you can do by simply change post type slug 'product' with you own post type slug 
add_action( 'pre_get_posts', 'wpse223576_search_woocommerce_only' );
function wpse223576_search_woocommerce_only( $query ) {
  if( ! is_admin() && is_search() && $query->is_main_query() ) {
    $query->set( 'post_type', 'product' );
  }
}
0

The code you have presented is just the output of the search. You will want to modify the search form itself.

Without being sure of your comfort level, I would recommend the article "How to Limit Your Wordpress Search Results" to walk you through that modification.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.