Changing a theme’s search function to only show results from woocommerce?

I am very new and trying to learn as I go.

I bought a premium theme from themeforest, without checking the functionality of various pages. Tried their support with no solution offered.

Now I am looking at the code with very little knowledge about it. Still trying to learn.

Can someone help me with this a bit:

1- I have identified three(code below) php files which might be able to solve this, I believe the first one to be the most relevant.

2- Since I am new can someone point out the right field to edit? Is it “( get_post_type() === ‘job_listing’) which needs to be changed?

Thanks.

searchform.php

<form class="search-form" method="get" action="<?php echo esc_url( home_url( "https://wordpress.stackexchange.com/" ) );
 ?>" role="search">
<?php if ( get_post_type() === 'job_listing' ) {
    echo '<input type="hidden" name="post_type" value="job_listing" />';
} ?>
<input class="search-field" type="text" name="s" id="s" placeholder="<?php esc_html_e
( 'What are you looking for?', 'listable' ); ?>" autocomplete="off" 
value="<?php the_search_query(); ?>"/>
<button class="search-submit" name="submit" id="searchsubmit"></button>

search.php:

<?php
/**
 * Search results archive
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package Listable
 */

 get_header(); ?>

<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
    <header class="page-header">
        <h1 class="page-title"><?php printf( esc_html__( 'Search results for: %s', 'listable' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
    </header>

    <?php if ( have_posts() ) : ?>

        <?php /* Start the Loop */ ?>

        <div class="postcards">
            <div class="grid">
                <?php /* Start the Loop */ ?>
                <?php while ( have_posts() ) : the_post(); ?>
                    <div class="grid__item  postcard">
                        <?php

                        /*
                         * Include the Post-Format-specific template for the content.
                         * If you want to override this in a child theme, then include a file
                         * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                         */
                        get_template_part( 'template-parts/content', get_post_format() );
                        ?>
                    </div>
                <?php endwhile; ?>
            </div>
            <?php the_posts_navigation(); ?>
        </div>

    <?php else : ?>
        <?php get_template_part( 'template-parts/content', 'none' ); ?>
    <?php endif; ?>

</main><!-- #main -->

search-job_listings.php

<?php
/**
* Search only listings results archive
*
* @link https://codex.wordpress.org/Template_Hierarchy
*
* @package Listable
*/

 get_header(); ?>

<div id="primary" class="content-area">
<div class="entry-content">
    <main id="main" class="site-main" role="main">
        <?php
        global $current_jobs_shortcode;

        $shortcode="[jobs keywords="". get_search_query() .'"        show_filters="true"';

        //get the listings page (Selected in the WPJM settings) job shortcode show_map parameter
        //we will apply it here also
        $show_map = listable_listings_page_shortcode_get_show_map_param();
        if ( false === $show_map ) {
            $shortcode .= ' show_map="false"';
        } else {
            $shortcode .= ' show_map="true"';
        }

        //get the listings page (Selected in the WPJM settings) job shortcode orderby parameter
        //we will apply it here also
        $orderby = listable_listings_page_shortcode_get_orderby_param();
        $shortcode .= ' orderby="' . $orderby . '"';

        //get the listings page (Selected in the WPJM settings) job shortcode order parameter
        //we will apply it here also
        $order = listable_listings_page_shortcode_get_order_param();
        $shortcode .= ' order="' . $order . '"';

        $shortcode .= ']';
        //save the shortcode so we can use it later to look at it's parameters in filters
        //this is because WPJM doesn't pass the parameters in some filters
        $current_jobs_shortcode = $shortcode;
        echo do_shortcode(  $shortcode );
        $current_jobs_shortcode = null;
         ?>
    </main><!-- #main -->
</div>
</div><!-- #primary -->

<?php
get_sidebar();
get_footer(); ?>

1 Answer
1

I’m not sure what the job_listing post type refers to – it’s probably part of a feature included in your theme – but in order to limit your search results by post type, the usual way to do this is by hooking into WordPress’ pre_get_posts action in your theme’s functions.php.

Because you’re new to this, a couple of other answers you should have a quick read of first:

  • WordPress actions and filters
  • Child themes

Then, armed with that knowledge, we’re going to hook into the pre_get_posts action in our functions.php (you can do this for testing purposes inside your current theme if you like, but as explained in the child themes answer above, your code will be overridden if and when you update your theme, so you should use a child theme to preserve it).

Open your functions.php and add:

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

You’ll find more details about how this works in the documentation for pre_get_posts. As explained in the first answer I linked to above, actions and filters (together known as ‘hooks’) allow you to modify the way WordPress core, plugins and themes work by either running code at just the right time or taking input, modifying it, and returning it. This is how the entire pluggable WordPress architecture works.

In this code snippet above, we’re modifying the WordPress query before it runs to query only posts of the type product. Since this is the post type WooCommerce uses internally to store products, you’ll then only get WooCommerce products returned (we also check of course that we’re actually doing a search; that we’re not in the admin panel; and that this is the main query – i.e. not a menu or other query – to make sure we don’t affect any more than we want to).

And that is how you can limit your searches to show WooCommerce product results only.

Leave a Comment