最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Search with filters

programmeradmin1浏览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.

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论