Limiting Wordpress Search function : Custom build

The website I am busy working on was built by another company that no longer supports Wordpress so I am looking for help

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

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

Share Improve this question asked Aug 23, 2019 at 12:06 Brad MontgomeryBrad Montgomery 1
Add a comment  | 

2 Answers 2

Reset to default 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' );
  }
}

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745211341a4616857.html

相关推荐

  • Limiting Wordpress Search function : Custom build

    The website I am busy working on was built by another company that no longer supports Wordpress so I am looking for help

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信