0

Wordpress - Custom Search on Custom Page while preserving the standard search function.

Hi all,

I would like to create a custom search on a custom page on top of main search function. The additional one is supposed to be focused only on products.

search-product.php

$product_type = $_GET['post_type'];

$args = array(
    'post_type' => $product_type,
    'posts_per_page' => -1
);

$the_query = new WP_Query($args);

/* Start the Loop */
while ($the_query -> have_posts() ) : $the_query -> the_post(); ?>


<h5><?php the_title(); ?></h5>

<?php endwhile; wp_reset_query(); ?>

The form:

<form role="search" method="get" class="search-form" action="<?php echo esc_url(home_url('/search-products/')); ?>">
        <div class="row align-items-center">
        <input type="hidden" name="post_type" value="product">
        <input type="text" name="productname" value="" autocomplete="off" placeholder="Product Name..." class="searchForm form-control"> 


            <button type="submit" class="btn btn-primary">Szukaj</button>

        </div>
    </form>

When I define the post type within the php file, it displays the query as expected. But I want to be able to modify the query on this page with URL instructions, however if I do something like this: http://localhost/wordpress/search-product/?post_type=product

WP returns the "page not found" page, with a different template. I believe it's the default "search" template.

Now... I know that I can/should modify the search.php file to make the whole thing work, BUT, I don't want to modify the main search.php (main query ? ).

I want to preseve the main search functionality, and have another custom one on top of it.

I have also utilized the template_include function as filter - however this returns a page, that visually is aligned with what I expect, however the head title is still "page not found".

I also tried to use the "pre_get_posts" hook, but also with no success.

I have spend lots of hours on this page to find the solutions, but all the answers include modifing the standard search.php file, but as mentioned, I do not want to modify the main search template.

Thanks!

  • So I suppose you already have created the custom product search page, and which you have assigned to be using the template named search-product.php? Can you add the search form code to your question? – Sally CJ May 1 '18 at 6:25
  • Hi Sally. Yes, you are correct. I have added the search form code. – theMaertin May 1 '18 at 6:51
  • Thanks for the update. Now in the form there, remove the <input type="hidden" name="post_type" value="product">, and in search-product.php, change $product_type = $_GET['post_type']; to $product_type = 'product';. Because I think the field's name (i.e. post_type) conflicts with the standard WP_Query request. So either you change the name (to something like cust_post_type) or do like I've suggested, which is hard-code the value in search-product.php. Hope that helps. – Sally CJ May 1 '18 at 7:14
  • 1
    I've renamed the input to another name, and binded it via get with the query. If I remove the input altogether, it redirects me to the missing results page too. Also another interesting observation - if I set the query to an empty wp_query, it returns only the page title. Like if there is a separate post index for that page, which only includes... of course, the current page. I like the idea with the additional value - sounds like an option. Maybe Ill then rewrite it to make it look "nice". Ill let you know. – theMaertin May 1 '18 at 12:32
  • 1
    I managed to find the solution Sally. This solved everything: function search_rewrite_rules() { global $wp_rewrite; $wp_rewrite->search_base = 'Search Product'; } add_action('search_rewrite_rules', 'template_chooser'); – theMaertin May 2 '18 at 18:37
2

It turned out, that the following solved my issue:

function search_rewrite_rules()
{
    global $wp_rewrite;
    $wp_rewrite->search_base        = 'Search Product';
}
add_action('init', 'search_rewrite_rules');

This hook moves the search_base to the given page, which enables everything I wanted to achieve.

Credit goes to the guy in this query: Custom slug in front of search URL

  • 1
    I think you meant $wp_rewrite->search_base = 'search-product'; and add_action( 'search_rewrite_rules', 'search_rewrite_rules' );.. But anyway, I'm glad you got it solved! – Sally CJ May 2 '18 at 19:40
  • Well I meant add_action( 'init', 'search_rewrite_rules' );. xD (was off to bed and extremely sleepy at that time..) – Sally CJ May 3 '18 at 1:16
  • 1
    Right! But I've figured it out ;) Thanks a lot for your support Sally! – theMaertin May 3 '18 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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