4 Comments

  1. Hi David,

    This is another post of you which I found very useful. If you don’t mind, I can suggest an improvement. Can make the code available for any theme, without the need of re-defining the categories inside the form by using – wp_dropdown_categories():

    `function dtwd_special_search_shortcode(){
    $args = array(
    ‘show_option_all’ => ‘ALL Categories’,
    ‘show_option_none’ => ”,
    ‘orderby’ => ‘NAME’,
    ‘order’ => ‘ASC’,
    ‘show_count’ => 0,
    ‘hide_empty’ => 1,
    ‘child_of’ => 0,
    ‘exclude’ => ”,
    ‘echo’ => 1,
    ‘selected’ => 0,
    ‘hierarchical’ => 0,
    ‘name’ => ‘cat’,
    ‘id’ => ”,
    ‘class’ => ‘special-search-select-box’,
    ‘depth’ => 0,
    ‘tab_index’ => 0,
    ‘taxonomy’ => ‘category’,
    ‘hide_if_empty’ => false,
    );
    ob_start(); ?>

    Search for articles using the form below
    <form method="get" id="searchform" class="searchform" action="” role=”search”>

    Search

    <?php $specialsearch = ob_get_clean();
    return $specialsearch;
    }
    add_shortcode('special-search', 'dtwd_special_search_shortcode');`

    In this way the form will get the categories directly from WordPress.

    • Hi Ciprian,

      so nice to see you here and thanks for such a wonderful suggestion.

      Thanks, David Tiong

      To implement this great alternative suggested setup, add the following code, before the line:
      ob_start(); ?>


      $args = array(
      'show_option_all' => 'ALL of Website',
      'show_option_none' => '',
      'orderby' => 'NAME',
      'order' => 'ASC',
      'show_count' => 0,
      'hide_empty' => 1,
      'child_of' => 0,
      'exclude' => '',
      'echo' => 1,
      'selected' => 0,
      'hierarchical' => 0,
      'name' => 'cat',
      'id' => '',
      'class' => 'special-search-select-box',
      'depth' => 0,
      'tab_index' => 0,
      'taxonomy' => 'category',
      'hide_if_empty' => false,
      );

      and delete all the code from:



      <select name="category_name" class="special-search-select-box"><option value ...


      to
      </select>

      and replace with

      <?php wp_dropdown_categories($args); ?>

  2. how to add custompost categery and custom field in this search form plz reply

    • Hi Vinay,

      If you are looking for advanced customisation in your search options, then consider using Relevanssi plugin, it is great for many search options, and can be customised further see http://www.relevanssi.com.

      You can customise the code above in the comments, to extend further to include post_type to search for custom post types, though it becomes a bit tricky to automatically pull in the custom post types. I’m working on a premium plugin which will incude search by category and search by custom post type.


Comments are closed for this article!