Skip to content
Advertisement

Search filter to show results on search page for custom post type

I have a filter that has select options for Wheel base and price. Wheel base is a custom taxonomy assigned to the vehicles post type. This taxonomy has two options SWB and LWB.

Here is the markup for my form:

<form class="filterOffers__form" role="search" action="<?php echo site_url('/search'); ?>" method="get" id="searchform">

  <fieldset class="filterOffers__fieldset filterOffers__wheelbase">
    <select class="filterOffers__select">
      <option value="">Wheel base</option>
      <option value="swb">Short wheel base</option>
      <option value="lwb">Long wheel base</option>
    </select>
  </fieldset>

  <fieldset class="filterOffers__fieldset filterOffers__minprice">
    <select class="filterOffers__select" name="min-price">
      <option value="">Min £ price</option>
      <option value="5000">£5000</option>
      <option value="10000">£10,000</option>
      <option value="20000">£20,000</option>
      <option value="30000">£30,000</option>
      <option value="40000">£40,000</option>
    </select>
  </fieldset>

  <fieldset class="filterOffers__fieldset filterOffers__maxprice">
    <select class="filterOffers__select" name="max-price">
      <option value="">Max £ price</option>
      <option value="10000">£10,000</option>
      <option value="20000">£20,000</option>
      <option value="30000">£30,000</option>
      <option value="40000">£40,000</option>
      <option value="50000">£50,000</option>
    </select>
  </fieldset>

  <fieldset class="filterOffers__fieldset d-none">
    <input type="hidden" name="post_type" value="vehicles" />
  </fieldset>


  <fieldset class="filterOffers__fieldset">
    <input class="filterOffers__submit" type="submit" alt="Search" value="Search" />
  </fieldset>

</form>

When the search button is clicked, I want to search the vehicles post type and display those vehicles that match the filtered query. For example, if I have Short wheel base and £5000 - £10,000 selected form the dropdowns, I want to display all the vehicles that match that query.

To do this, I have created a search.php file, which looks like this:

<?php 
/*
Template Name: Search Page
*/

get_header();

  global $query_string;

  $query_args = explode("&", $query_string);
  $search_query = array();

  foreach ($query_args as $key => $string){
    $query_split = explode("=", $string);
    $search_query[$query_split[0]] = urldecode($query_split[1]);
  }

  $the_query = new wp_query($search_query);

  if( $the_query -> have_posts() ) : ?>
    <h3>Search Result for : <?php echo htmlentities($s, ENT_QUOTES, 'UTF-8'); ?> </h3>

    <?php while ( $the_query->have_posts() ) : $the_query->the_post();
      echo the_title();
    endwhile; wp_reset_postdata();

  else: ?>

    <h2><?php _e("no posts found"); ?></h2>

  <?php endif;

get_footer(); ?>

Current behaviour:

If from the dropdown I select Short wheel base and £5000 - £10,000 and click the search button, it takes me to:

/search?wheel-base=swb&min-price=5000-10000&max-price=&post_type=vehicles

The above url is correct based on my search parameters. However, the page 404s.

  • When I’m on /search, it’ll echo the “Search Result for :” text.
  • When I’m on /search?wheel-base=swb&min-price=5000-10000&max-price=&post_type=vehicles – it’ll 404.

I have followed the WordPress codex to create a search page (a page for search exists in the admin with the URL /search and the search page template is applied).

Ideas on what I’m missing here?

Advertisement

Answer

Based on this post : https://wordpress.stackexchange.com/a/291661/205128

post_type appears to be a reserved name in wordpress (list)

Can you try to change post_type input name, or prefix it like mynamespace_post_type ?

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement