[Support request] Displaying Products in Site Search Results

Home Forums Support Displaying Products in Site Search Results

This topic contains 7 replies, has 2 voices, and was last updated by  Mike 6 days, 9 hours ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #645165

    Mike

    Hello,

    I am having trouble getting products to display nicely on the site search results page.

    At present they display as normal blog posts and it looks totally wrong.

    My solution to this for now has been to exclude products from the site search and display a message saying use the product search form on the shop page if looking to search products.

    However, do you know a more elegant solution? I am thinking maybe a couple of tabs at the top of the page that the user can flick between to display either posts and pages and another tab which displays products, but displays the products in the same format as the woo commerce product pages do.

    A bit of a complex one I guess, but I just hate the way the products display in search results!

    Many Thanks,
    Mike

    P.S, Tom, check the support email, I have sent something!

    GeneratePress 2.1.3
    GP Premium 1.6.2
    #645221

    Mike

    Hello,

    I have tried adding the following code into the “Inside Content Container”,

    <?php if ( is_search() ) { ?>
    
    <div class="tab">
      <h3> Search For : </h3>
      <button class="tablinks" onclick="location.href='/?s=<?php echo get_search_query(); ?>'">Posts & Pages</button>
      <button class="tablinks" onclick="location.href='/?s=<?php echo get_search_query(); ?>&post_type=product'">Products</button>
    </div>
    
    <?php } ?>
    

    It sort of works (does not account for other query strings), however I dont think its the way to go…

    Ideally I guess I want users to search something using the main wordpress ?s= method and get a heading for products and a heading for news etc all displaying the relevant posts below and in the relevant format (e.g woo commerce products format for the products heading).

    Maybe I am just over thinking it and should either remove the menu bar search all together or just allow it to only search posts and pages, not products.

    A perfect case of web development and design clashing!

    Thanks,
    Mike

    #645269

    Mike

    Hello,

    I am thinking more and adding to this thread.

    Is there a way to make the menu bar search in the products by default instead of the site? e.g append “&post_type=product” to the query string.

    Thanks,
    Mike

    #645293

    Mike

    Hello,

    Another update, I did a search of the forums and found the following code that answers my above question,

    add_filter( 'generate_navigation_search_output', 'tu_wc_navigation_search' );
    function tu_wc_navigation_search() {
    	printf(
    		'<form method="get" class="search-form navigation-search" action="%1$s">
    			<input type="search" class="search-field" value="%2$s" name="s" title="%2$s">
    			<input type="hidden" name="post_type" value="product">
    		</form>',
    		esc_url( home_url( '/' ) ),
    		esc_attr( get_search_query() ),
    		esc_attr_x( 'Search', 'label', 'generatepress' )
    	);
    }
    

    So I now have the nav search searching only products, which is fantastic.

    But it does make me wonder, is there a way to display both pages & posts aswell as products on the same page as a search result however keeping the correct format…

    I guess it would need PHP code that could loop through the site search results and filter products into one table formatted the same as the store and news & posts into another standard table as appears by default.

    Thanks,
    Mike

    #645420

    Tom Lead Developer

    Hey Mike,

    Glad you found that navigation search solution.

    It would likely involve a decent amount of custom coding to have products and other post types all displaying the same in the search results. I’m sure it’s possible, but it may be a bit out of the scope of this forum.

    Have you done much searching around? There might be a solution out there already.

    Let me know 🙂

    #645906

    Mike

    Hi Tom,

    I have done a fair bit of searching around.

    I think for now I am gonna just leave it searching for products only.

    Dont suppose you could let me know what you think of the site navigation in general? Do you think its fairly intuitive to find products?

    Also, did you get my email about the GP licensing?

    Thanks,
    Mike

    #646051

    Tom Lead Developer

    Just took a look around – worked nicely for me 🙂

    I did – sorry for the delay in getting back to you. Been a busy week getting ready for GPP 1.7.

    #646807

    Mike

    Hello Tom,

    Thats good to hear, I was afraid my menu system was too complex!

    I am very much looking forward to GPP 1.7!

    Thanks,
    Mike

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.