最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Limiting Wordpress Search function : Custom build

programmeradmin1浏览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

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.

发布评论

评论列表(0)

  1. 暂无评论