0

right now i have a problem in the search result page (search.php) the result is shown by the index page template not its template and it display all the posts despite it have what the searcher enter or not

my index.php page

<?php get_header(); ?>
<?php
$search = $_get['search'];
$args = array(
    'post_title_like' => $search
);
$res = new wp_query($args);
if ($res -> have_posts()){
    while ($res -> have_posts()) {
        $res -> the_post();
        ?>

        <!--start section 1-->
        <section class="section1" style="margin: 0;box-shadow: 0 2px 10px">
            <div class="container">
                <a href="<?php echo get_post_permalink(); ?>">
                    <div class="img">
                        <?php the_post_thumbnail(); ?>
                    </div>
                    <h1 class="page-title"><?php the_title(); ?></h1>
                </a>
                <div class="clearfix"></div>
            </div>
        </section>
        <!--end section 1-->

        <?php the_content(); ?>

        <?php
    }
}
?>
<?php get_footer() ?>

my search.php page

<?php get_header(); ?>
<?php
if (have_posts()){
    while (have_posts()) {
        the_post();
        ?>
        <section class="section1" rule="main">
            <div class="container">
                <div class="img">
                    <?php the_post_thumbnail(); ?>
                </div>
                <h1 class="page-title"><?php the_title(); ?></h1>
                <div class="clearfix"></div>
            </div>
        </section>
        <?php
    }
}
?>
<?php get_footer(); ?>

if you need more code please tell me

0

The search.php template is used, and the correct posts returned, when a search is performed with the ?s= query. Not ?search=. Make sure the name attribute of the input in your search form is s:

<input type="search" name="s">

And your index.php file should absolutely not be using a custom query like that. Use the main loop the same way you have in search.php. WordPress already queries the correct posts for you.

if ( have_posts() ){
    while ( have_posts() ) {
        the_post();
    }
}
  • can i ask 1 more question – abdo sayed Nov 23 '18 at 12:26
  • the search is fine now but when i add a filter say so – abdo sayed Nov 23 '18 at 12:27
  • wp_query(array('post_type' => 'page') it gives me all pages not just the page i search for – abdo sayed Nov 23 '18 at 12:29
  • You shouldn't be using custom queries like that in these templates. If you do that you're doing a new query for all pages that has nothing to do with the template. If you only want to search pages do a search of this site, people have explained how to do that properly. – Jacob Peattie Nov 23 '18 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.