• Resolved mizfa

    (@mizfa)


    I have been created custom search form in my WordPress theme. She looks like:

    
    <form role="search" class="form" method="get" action="<?php echo home_url('/');?>">
      <input type="search" id="keyword" class="inp-search" onclick="showNewTag()" oninput="searchTags()" onkeyup="fetch()" placeholder="search by tag" value="<?php echo get_search_query() ?>" name="s" title="Search" />
    
    

    And i realized custom AJAX search by POST title. But my task is search by tags. I don’t know how i can do that. My search by post title located in functions.php and looks like:

    
    add_action( 'wp_footer', 'ajax_fetch' );
    function ajax_fetch() {
    ?>
    <script type="text/javascript">
      function fetch(){
    
        jQuery.ajax({
        url: '<?php echo admin_url('admin-ajax.php'); ?>',
        type: 'post',
        data: { action: 'data_fetch', keyword: jQuery('#keyword').val() },
        success: function(data) {
            jQuery('#grid').html( data );
        }
    });
    
    }
    </script>
    
    <?php
    }
    
     add_action('wp_ajax_data_fetch' , 'data_fetch');
     add_action('wp_ajax_nopriv_data_fetch','data_fetch');
     function data_fetch(){
    
       $the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => 
     esc_attr( $_POST['keyword'] ), 'post_type' => 'post' ) );
    if( $the_query->have_posts() ) :
        while( $the_query->have_posts() ): $the_query->the_post(); 
    
            if(get_field('type') == "box") {
                ?>
                <div class="grid-item grid-item--<?php echo get_field('type');?> <?php echo get_field('position');?>" data-tooltitle="Hood Baroque" data-tooltip="Design | 3D modeling | Drawings">
                    <a class="linkpost" href="<?php echo get_post_permalink()?>"><img src="<?php the_field('picture');?>" alt=""></a>
                    <a href="<?php echo get_post_permalink()?>" class="text">
                        <img src="<?php the_field('svg_picture');?>" alt="">
                    </a>
                </div>
        <?php
    
            } else {
        ?>
        <div class="grid-item grid-item--<?php echo get_field('type');?> left" data-tooltitle="Hood Baroque" data-tooltip="Design | 3D modeling | Drawings">
            <a class="linkpost" href="<?php echo get_post_permalink()?>"><img src="<?php echo get_field('picture');?>" alt=""></a>
                <a href="<?php echo get_post_permalink()?>" class="text">
                    <img src="<?php echo get_field('svg_picture');?>" alt="">
                </a>
        </div>
    
        <?php 
            }
    
         endwhile;
        wp_reset_postdata();  
    endif;
    
    die();
    }
    
    

    How i can realize search by tags ONLY?

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom AJAX search by tags WP’ is closed to new replies.