Search by Title in WordPress

The below-given script structure can be used for the implementation of Search by Title in WordPress.

Reloading the section to avoid page reload.

<script>
        document.cookie="profile_viewer_uid=";
                $('input[name="blog-inner-search"]').keyup(function(){
                   $("#blog-post-filter").load(location.href + " #blog-post-filter");
                  document.cookie="profile_viewer_uid="+$(this).val();
                  });
                  setTimeout(function () {
                 $("#blog-post-filter").load(location.href + " #blog-post-filter");
                    }, 2000);

        </script>


        <div id="blog-post-filter">


            <!--row 1-->

            <?php
                $search_filter= ($_COOKIE['profile_viewer_uid'])? $_COOKIE['profile_viewer_uid']:'';
                $args = array(
                'post_type'=> 'post',
                'orderby'    => 'ID',
                'post_status' => 'publish',
                'order'    => 'DESC',
                'tag_slug__in'    => array('popular'),
                's' => $search_filter,
                'posts_per_page' => 3 // this will retrive all the post that is published 
                );
                $result = new WP_Query( $args );
                if ( $result-> have_posts() ) : ?>
                <?php while ( $result->have_posts() ) : $result->the_post(); ?>

                        ...............
                        ...............
                        ...............
            <?php endwhile; else: ?>
        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
        <?php endif; wp_reset_postdata(); ?>

        </div>

</div>

Leave a comment

Your email address will not be published. Required fields are marked *