• Resolved jerujanssen

    (@jerujanssen)


    I have the following problem. When I get one result of a searchquery, it shows one correct result, but when I try to loop the results – it just loops all posts.

    I use a highly customized shaken-grid-free template. In the header I inserted a searchform like this:

    <form id="searchform" name="searchform" method="get" action="<?php echo home_url(); ?>">
    		<div>
    			<input type="text" id="s" name="s" class="sfield" value="type your search query here" onfocus="if(this.value == 'type your search query here') { this.value = ''; }" />
    			<input type="submit" id="searchsubmit" value="<?php esc_attr_e( 'Search'); ?>" class="doSearch"/>
    		</div>
        </form>

    To collect the results I have this code, which works for one result, but how do I loop the results? When I use this:
    <?php while (have_posts()) : the_post(); ?>
    It will just show random posts.

    <?php /* If there are no posts to display, such as an empty archive page  */ ?>
    <?php if (have_posts()) : ?>
    <?php #$my_query = new WP_Query('posts_per_page=2'); ?>
    <?php wp_reset_postdata(); // reset the query ?>
    
    <?php
    echo "Quotes with ". $_GET['s'];
    ?>
    
    <?php #while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <?php #while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <center>
    	<div class="boxxy">
        	<?php
    		if ( has_post_thumbnail() ){ ?>
    			<?php $thumbID = get_post_thumbnail_id($post->ID); ?>
                <a href="<?php echo get_post_meta($post->ID, 'URL', true); ?>" title="<?php the_title(); ?>" target="_blank">
                    <?php the_post_thumbnail(); ?>
                    <span class="view-large"></span>
                </a>
            <?php } ?>
            <?php the_content('<p>Continue Reading &rarr;</p>'); ?><br /><br />
    		<?php if(function_exists('the_ratings')) { the_ratings(); } ?><br /><br /><br />
            <?php edit_post_link('Edit this post'); ?>
        </div>
    	</center>
    <?php
    #endwhile;
    // Reset Post Data
    wp_reset_postdata();
     ?>
    
    <?php else : ?>
    <div id="sort">
    <div class="box">
    	<h2>Sorry, no posts were found</h2>
        <?php get_search_form(); ?>
    </div>
    </div><!-- #sort -->
    <?php endif; ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • Make sure when you are calling your while statement you are referring to your query. Instead of $the_query->have_posts() use $my_query->have_posts().

    Also, place the new WP_Query call above everything else!

    <?php
    $my_query = new WP_Query( 'posts_per_page=2' );
    
    	if( $my_query->have_posts() ) :
    
    		while( $my_query->have_posts() ) : $my_query->the_post(); ?>
    			<div style="text-align: center">
    				<div class="boxxy">
    					<?php
    					if ( has_post_thumbnail() ){ ?>
    						<?php $thumbID = get_post_thumbnail_id($post->ID); ?>
    						<a href="<?php echo get_post_meta($post->ID, 'URL', true); ?>" title="<?php the_title(); ?>" target="_blank">
    							<?php the_post_thumbnail(); ?>
    							<span class="view-large"></span>
    						</a>
    					<?php } ?>
    					<?php the_content('<p>Continue Reading →</p>'); ?><br /><br />
    					<?php if(function_exists('the_ratings')) { the_ratings(); } ?><br /><br /><br />
    					<?php edit_post_link('Edit this post'); ?>
    				</div>
    			</div>
    <?php
    		endwhile;
    
    	else : ?>
    		<div id="sort">
    		<div class="box">
    			<h2>Sorry, no posts were found</h2>
    			<?php get_search_form(); ?>
    		</div>
    		</div><!-- #sort -->
    <?php
    	endif;
    	wp_reset_postdata();
    ?>
    Thread Starter jerujanssen

    (@jerujanssen)

    Thanks toocoolone for your reply.
    The problem is: when I do a wp_query like this:
    $my_query = new WP_Query( ‘posts_per_page=2’ );

    It will loose the current search and just displays two random posts.

    Thread Starter jerujanssen

    (@jerujanssen)

    Some additional info:

    When I don’t do this: wp_reset_postdata();
    The one correct result also disappears.

    If I do this loop:
    while (have_posts()) : the_post();
    It also displays random posts.

    Okay, I think I understand better what you’re trying to do. You should be able to control the pagination like this. Just make sure you’re passing the search string with the new query. What you’re doing is replacing the default query which is searching correctly with a totally new one, which only shows two random pages because the search string isn’t being passed to it.

    Check out the WP_Query codex for more options. You can pass as many as you want by separating them with a &.

    Remove the line:

    $my_query = new WP_Query(  'posts_per_page=2'  );

    And replace it with this:

    $my_query = new WP_Query(  'posts_per_page=2&s=' . $_GET['s']  );

    Thread Starter jerujanssen

    (@jerujanssen)

    Yes, that’s it!
    Thanks again, this works great.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Search results : only one result shows up’ is closed to new replies.