• Resolved stephanium

    (@stephanium)


    I wrote my own search-function and got some problem now with displaying the results… search.php only gets called, when there are no results to display

    When I delete everything in the “if( have_posts() )”-clause, nothing changes when there are posts found… the posts are somehow still showing up (when I delete that part, nothing should show up – i guess). When I delete everything in the “else”-clause, the “Nothing found” message doesn’t appear anymore when no Posts are found.

    Here’s my search function ($include_posts contains the IDs of the Posts which match the search criteria):

    if(!empty($include_posts))
    {
    	$args = array(
    		'post__in' => $include_posts,
    		'is_search' => true
    	);
    	wp_reset_query();
    	query_posts($args);
    }
    else
    {
    	wp_reset_query();
    }

    And here’s the code of search.php:

    <?php if ( have_posts() ) : ?>
    
    	<header class="page-header">
    		<h1 class="page-title"><?php printf( __( 'Suchergebnisse f&uuml;r: %s', 'twentytwelve' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
    	</header>
    
    	<?php twentytwelve_content_nav( 'nav-above' ); ?>
    
    	<?php /* Start the Loop */ ?>
    	<?php while ( have_posts() ) : the_post(); ?>
    		<?php get_template_part( 'content', get_post_format() ); ?>
    	<?php endwhile; ?>
    
    	<?php twentytwelve_content_nav( 'nav-below' ); ?>
    
    <?php else : ?>
    
    	<article id="post-0" class="post no-results not-found">
    		<header class="entry-header">
    			<h1 class="entry-title"><?php _e( 'Keine Suchergebnisse', 'twentytwelve' ); ?></h1>
    		</header>
    
    		<div class="entry-content">
    			<p><?php _e( 'Bitte versuchen Sie es erneut mit anderen Suchw&ouml;rtern.', 'twentytwelve' ); ?></p>
    			<?php get_search_form(); ?>
    		</div><!-- .entry-content -->
    	</article><!-- #post-0 -->
    
    <?php endif; ?>

Viewing 9 replies - 1 through 9 (of 9 total)
  • Where have you defined the search function?

    Thread Starter stephanium

    (@stephanium)

    In functions.php of my theme

    Did’nt understand it.
    In WordPress you cant define the searhc function. You can just modify it by use of a few filters.

    What exactly are you doing in function.php?

    Can you post the full code of your search function from functions.php?

    Thread Starter stephanium

    (@stephanium)

    Because I had some problems with my search-engine (it never found any results) I wrote an complete new search-function:

    function searchFilter($query) {
    	if ($query->is_search)
    	{
    		query_posts();
    
    		$searchterm = strtolower($_GET['s']);
    		$include_posts = array();
    
    		if (have_posts())
    		{
    			while (have_posts())
    			{
    				the_post();
    
    				$title = strtolower(get_the_title());
    				$content = strtolower(get_the_content());
    
    				if(strpos($title, $searchterm) || strpos($content, $searchterm))
    				{
    					$include_posts[] = get_the_ID();
    				}
    			}
    		}
    
    		if(!empty($include_posts))
    		{
    			$args = array(
    				'post__in' => $include_posts,
    				'is_search' => true
    			);
    			wp_reset_query();
    			query_posts($args);
    
    		}
    		else
    		{
    			wp_reset_query();
    		}
    	}
    }
    add_filter('pre_get_posts', 'searchFilter');

    I first wanted to filter the posts with the parameter ‘s’ in $args. But when using that parameter I always ended up with an HTTP 500 Error.

    Quite a few things going wrong here. Lets take them one by one.

    1. Why did you need to write your own search function?
    Can you give an example of what search term you were using, what you expected to see in search results and what did you get instead that you did not like/expect?

    2. The filter pre_get_posts is used to modify the queryu variables; not execute an entirely new query.

    3. Instead you could comment the above function, and in your search.php, use a new WP_Query with the required arguments.

    // The Query
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) {
    	echo '<ul>';
    	while ( $the_query->have_posts() ) {
    		$the_query->the_post();
    		echo '<li>' . get_the_title() . '</li>';
    	}
    	echo '</ul>';
    } else {
    	// no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();

    But before modifying search.php, lets focus on why you need to modify the default search query. Does it not return search results?
    If not, what did it return?

    Thread Starter stephanium

    (@stephanium)

    I didn’t get any results with the default search after an WP Update. After lots of Troubleshooting and researching I decided to just write an own function.
    I read, the parameter ‘s’ should execute a “normal” search through the Posts. So tried to get results with something like this:

    $args = array(
    	's' => $searchterm
    );
    query_posts($args);

    But when using the Parameter ‘s’ I always ended up with an HTTP Error 500

    I read that search.php is only there for displaying the results. But when that’s easier, I’ll try to modify the query directly in search.php

    Thread Starter stephanium

    (@stephanium)

    I solved the problem with a workaround in search.php like you suggestet.
    I completely removed the search function in functions.php and my search.php looks like this now:

    <?php
    $my_query = new WP_Query();
    $my_query->get_posts();
    $searchterm = strtolower($_GET['s']);
    $i = 0;
    
    while($my_query->have_posts())
    {
    	$my_query->the_post();
    
    	$title = strtolower(get_the_title());
    	$content = strtolower(get_the_content());
    
    	if(strpos($title, $searchterm) || strpos($content, $searchterm))
    	{
    		if($i == 0)
    		{
    			print '<header class="page-header">';
    				print '<h1 class="page-title">'; printf( __( 'Suchergebnisse f&uuml;r: %s', 'twentytwelve' ), '<span>' . get_search_query() . '</span>' ); print '</h1>';
    			print '</header>';
    			twentytwelve_content_nav( 'nav-above' );
    		}
    		get_template_part( 'content', get_post_format() );
    		$i++;
    	}
    }
    if($i == 0)
    {?>
    	<article id="post-0" class="post no-results not-found">
    		<header class="entry-header">
    			<h1 class="entry-title"><?php _e( 'Keine Suchergebnisse', 'twentytwelve' ); ?></h1>
    		</header>
    
    		<div class="entry-content">
    			<p><?php _e( 'Bitte versuchen Sie es erneut mit anderen Schlagw&ouml;rtern.', 'twentytwelve' ); ?></p>
    			<?php get_search_form(); ?>
    		</div><!-- .entry-content -->
    	</article><!-- #post-0 -->
    <?php }
    else
    {
    	twentytwelve_content_nav( 'nav-below' );
    }
    ?>

    Thank you very much for your help, shariqkhan2012!

    Thread Starter stephanium

    (@stephanium)

    Problem solved.

    Glad that you issue is solved. Have a good day ahead.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘search.php only gets called when no results appear’ is closed to new replies.