Search returns only the home of the site

Asked

Viewed 609 times

2

I created the files search.php and searchform.php for a Wordpress theme that I am developing. However when testing the search it does not return anything, showing only the home page.

I had written a different pattern query in another theme of an old project and in this theme it works normally, but in my current project this error occurs. The codes are as follows::

searchform.php

<form action="<?php bloginfo('home'); ?>" method="get" accept-charset="utf-8" id="searchform" role="search">
    <input type="text" id="campo_busca" name="busca" placeholder="Buscar" value="<?php the_search_query(); ?>"></input>
    <input id="button_busca" type="submit"></input>
</form>

search php.

<?php
/*
Template Name: Search Page
*/
?>

<?php get_header(); ?>
<main>
    <div class="content">
      <section id="conteudo_pagina">
        <p id="tempobusca"><?php $mySearch = & new WP_Query("s=$s & showposts=20"); $num = $mySearch->post_count; echo $num.' resultados de pesquisa para '; the_search_query();?> em <?php  get_num_queries(); ?> <?php timer_stop(1); ?> segundos.</p>

        <?php if ( have_posts() ) : ?>
            <?php while ( have_posts() ) : the_post(); ?>
            <div class="box internas">
              <div class="cabecalho">
                <h2>BUSCA</h2>
              </div>
            </div>
            <ul id="linha_pesquisa">
              <li>
                <article>
                    <p class="titulobusca"><a href="<?php the_permalink() ?>" rel="bookmark" title="Link permanente para <?php the_title(); ?>"><?php the_title(); ?></a></p>
                    <p class="excertobusca"><?php the_content_rss('more_link_text', TRUE, '', 30); ?></p>
                </article>
              </li>
            </ul>
        <?php endwhile; ?>
            <div id="paginacao">
                <?php if(function_exists('wp_pagenavi')) : ?>
                    <?php wp_pagenavi() ?>
                <?php else : ?>
                    <div class="maisantigos"><?php next_posts_link(__('&laquo; Mais antigos','arclite')) ?></div>
                    <div class="maisrecentes"><?php previous_posts_link(__('Mais recentes &raquo;','arclite')) ?></div>
                <?php endif; ?>
            </div>
        <?php else:?>
            <ul id="linha_pesquisa">
              <li>
                <article>
                    <h3>404 - NOT FOUND</h3>
                    <h4>Post não encontrado</h4>
                    <p>Desculpe, mas o texto que você procura não está aqui.</p>
                </article>
              </li>
            </ul>
        <?php endif; ?>
      </section>
    <?php get_sidebar(); ?>
  <?php get_footer(); ?>

How can I solve this problem?

1 answer

2


I don’t know why you’re wearing a page template when the default is that Theme has a filing cabinet search.php dedicated to display search results. Comparing your code with your Twenty Eleven I see that the searchform.php has the following problems:

  • bloginfo() is depressed, if you turn on the WP_DEBUG see the warning. Use

     action="<?php echo esc_url( home_url( '/' ) ); ?>"
    
  • you defined the field search like name="busca", but is looking for s=$s in his search.php

I have not tried creating a page template for search.php and I actually suspect that something like this could create conflict, the ideal would be page-search.php and configure whatever it takes to make it work. Using search.php pattern works.

Your search.php has a & left in:

$mySearch = & new WP_Query("s=

This jumps a warning:

Deprecated: Assigning the Return value of new by Reference is deprecated in [...]

  • 1

    Thank you very much! I made the corrections and the search came back to work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.