Search with filters

I want to create Three Filters in the search form with three different filters as (Tags, categories and posts). Is it po

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.

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.

Share Improve this question asked Aug 20, 2019 at 5:01 saicharansaicharan 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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/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/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/manual/en/getting-started.php, which you can have a look at.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745225178a4617427.html

相关推荐

  • Search with filters

    I want to create Three Filters in the search form with three different filters as (Tags, categories and posts). Is it po

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信