V
V
Vladislav Ignatiev2018-02-15 20:09:10
MySQL
Vladislav Ignatiev, 2018-02-15 20:09:10

How to customize search in Wordpress template?

Hello!
I've been banging my head against the monitor for days now. Help with the search.
I have my own template for WP, everything is pulled and everything works as it should, I created a custom post type , one of these post types is called post_type_products . It stores records with metakmi. What is the actual problem ?
The problem is that my search looks for records by titles , but I need to search by tags and give out my thumbnails that I set up.
search.php file

The code
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
    <?php if(isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
           if($type == 'post_type_products') {?>
               <?php ale_part('postpreview_products'); ?>
           <?php } else { ?>
               /* Format for custom post types that are not "book,"
               or you can use elseif to specify a second post type the
               same way as above. Copy the default format here if you
               only have one custom post type. */
           <?php } ?>
    <?php } else { ?>
              /* Format to display when the post_type parameter
              is not set (i.e. default format) */
<?php } ?>
<?php endwhile; else: ?>
<div class="container">
  <div class="product_title_no">
    <h1>
      Не найдено
    </h1>
</div>
</div>
<?php endif; ?>
The funcion.php file here is the filtering function by post type - post_type_products
The code
function searchfilter($query) {
    if ($query->is_search && !is_admin() ) {
        if(isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
                if($type == 'post_type_products') {
                    $query->set('post_type',array('post_type_products'));
                }
        }       
    }
return $query;
}
add_filter('pre_get_posts','searchfilter');
Search Form
The code
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
                    <label>
                        <input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Введите название', 'placeholder' ) ?>" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />
                        <input type="hidden" name="post_type" value="post_type_products" />
                    </label>
                    <input type="submit" class="search-submit" value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" />
                </form>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2018-02-15
@vladislavignatyev

I must admit that I did not have to implement a search (by the way, I plan one of these days)
Suddenly the following links will be useful for you in solving this issue:
https://www.rfmeier.net/include-category-and-post-...
https://www.reddit.com/r/Wordpress/comments/20nhq5...
www.secretsofgeeks.com/2014/09/wordpress-search-ta...
https://wordpress.stackexchange.com/questions/ 1728...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question