0

I want to create Three Filters in the search form with three different filters as (Tags, categories and posts). Is it possible to create those filters? If possible means how can I create that?

Basically, am not good at PHP. So, Please help me out of this.

Thanks.

0

Here is one to create a search form with tag and category filters. For filtering posts this form uses the default text input way, where user can type some search phrase that is matched against post titles and content.

1) Added to theme's functions.php

function get_tag_ids_and_names() {
  $out = array();
  $arga = array(
    // check https://codex.wordpress.org/Function_Reference/get_tags for available params
  );
  $terms = get_tags( $arga );
  if ( $terms ) {
    foreach ( $terms as $term ) {
      $out[$term->term_id] = $term->name;
    }
  }
  return $out;
}

function get_category_ids_and_names() {
  $out = array();
  $arga = array(
    // check https://developer.wordpress.org/reference/functions/get_categories/ for available params
  );
  $terms = get_categories( $arga );
  if ( $terms ) {
    foreach ( $terms as $term ) {
      $out[$term->term_id] = $term->name;
    }
  }
  return $out;
}

function render_select_options( array $options ) {
  if ( $options ) {
    foreach ( $options as $value => $name ) {
      printf(
        '<option value="%s">%s</option>',
        esc_attr( $value ),
        esc_html( $name )
      );
    }
  }
}

/**
  * Custom tempalte function that renders a search form
  * text input for searching posts by search phrase, searches by title and content by default
  * optional dropdown select filter for post tag
  * optional dropdown select filter for category
  * searches only posts with post post_type
  */
function search_form_with_fitlers() {
  $tags = get_tag_ids_and_names();
  $categories = get_category_ids_and_names();
  printf(
    '<form id="search-with-filters" method="GET" action="%s">
    <input type="text" name="s" value="%s">
    <select name="post_tag"><option value="">--</option>%s</select>
    <select name="category"><option value="">--</option>%s</select>
    <input type="hidden" name="post_type" value="post">
    <input type="submit" value="%s">
    </form>',
    home_url( '/' ),
    get_search_query(),
    render_select_options( $tags ),
    render_select_options( $categories ),
    esc_html__( 'Search', 'text-domain' )
  );
}

You could also do the things done in search_form_with_fitlers function in your searchform.php file.

2) use in post / page templates or where needed

<?php search_form_with_fitlers(); ?>

P.s. You can find many good beginner level tutorials and courses online for learning PHP, if you haven't searched or started one yet. The PHP manual has also an introduction to the language, https://www.php.net/manual/en/getting-started.php, which you can have a look at.

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.