WordPress.org

Ready to get started?Download WordPress

Forums

Search results page and additional query conflict (4 posts)

  1. lukeshumard
    Member
    Posted 3 years ago #

    I've created a custom template for my search results (search.php), which is working fine except for the fact that my sidebar that contains an additional query is not displaying properly. I call on the same sidebar several times in the site and it works in every instance except for the search results page. I'm sure this has something to do with the way I'm calling the search query, but I don't know what I'm doing wrong. The rest of the page displays fine, so I'm fairly certain it's not a PHP or syntax error. Suggestions?

    <div id="content">
    <h2>Search Results for <?php /* Search Count */ $key = wp_specialchars($s, 1); _e('<span class="search-terms">'); echo $key; _e('</span>'); wp_reset_query(); ?></h2>
    
    	<?php
    		$wp_query->is_archive = false;
    		$wp_query->is_home = true;
    		$wp_query->query();
    		if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    
    			<div id="post-<?php the_ID(); ?>">
    				<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    
    				<div class="entry evententry<?php if(($wp_query->current_post + 1) == ($wp_query->post_count)) {echo ' bottom';};?>">
    					<?php the_excerpt(''); ?>
                        <?php if (function_exists('sociable_html')) { echo sociable_html(); }; ?>
    				</div>
    
    			</div>
    
    		<?php endwhile; ?>
    
    		<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
    
    	<?php else : ?>
    
    		<h3>No posts found. Try a different search?</h3>
    
    	<?php endif; wp_reset_query(); ?>
    
    </div>
    
    <div class="sidebar" id="rightsidebar">
    
    <h2>Search</h2>
    
    <div id="search">
    <form method="get" id="searchform" action="<?php echo get_option('home'); ?>">
    	<div>
        <input type="text" value="Search for an event, venue, etc." onfocus="value=''" name="s" id="s" /><br />
    	<input type="submit" id="searchsubmit" value="Search the Paddington Festival" />
    	</div>
    	</form>
    
        <div class="clear"></div>
    </div>
    
    <h2>Upcoming Events</h2>
    	<?php $date_day = date('d');
    		$date_month = date('m');
    		$date_year = date('Y');
    		$today = $date_year . '-' . $date_month . '-' . $date_day;
    	$wp_query = new WP_Query('cat=3&posts_per_page=4&meta_key=Event-Date&meta_compare=>=&meta_value=' . $today . '&orderby=meta_value&order=asc');
        if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post();?>
        	<div class="sidebarentry<?php if(($wp_query->current_post + 1) == ($wp_query->post_count)) {echo ' bottom';};?>">
            <a href="<?php the_permalink(); ?>"><?php echo get_image('event-photo'); ?></a>
            <p><?php the_title(); ?><br /><?php echo get('Event-Venue'); ?><br /><?php echo get('Event-Date'); ?></p>
            </div>
        <?php endwhile; endif; wp_reset_query(); ?>
    
    </div>
  2. stvwlf
    Member
    Posted 3 years ago #

    Give your sidebar query variable a name other than $wp_query, such as $sidebar_query. Using the same variable name, you are overwriting one query with the other.

    see this article
    http://weblogtoolscollection.com/archives/2008/04/13/define-your-own-wordpress-loop-using-wp_query/

  3. lukeshumard
    Member
    Posted 3 years ago #

    That actually didn't work. I had to switch it to...

    query_posts('cat=3&posts_per_page=4&meta_key=Event-Date&meta_compare=>=&meta_value=' . $today . '&orderby=meta_value&order=asc');
        if (have_posts()) : while (have_posts()) : the_post();?>

    Do you know why replacing $wp_query as $sidebar_query didn't work, where as switching it to query_posts did?

  4. lukeshumard
    Member
    Posted 3 years ago #

    Also, for documentation purposes, here's my updated code.

    <?php $date_day = date('d');
    		$date_month = date('m');
    		$date_year = date('Y');
    		$today = $date_year . '-' . $date_month . '-' . $date_day;
    	$sidebar_query = new WP_Query();
    	$sidebar_query->query('cat=3&posts_per_page=4&meta_key=Event-Date&meta_compare=>=&meta_value=' . $today . '&orderby=meta_value&order=asc');
        if ($sidebar_query->have_posts()) : while ($sidebar_query->have_posts()) : $sidebar_query->the_post();?>
        	<div class="sidebarentry<?php if(($sidebar_query->current_post + 1) == ($sidebar_query->post_count)) {echo ' bottom';};?>">
            <a href="<?php the_permalink(); ?>"><?php echo get_image('event-photo'); ?></a>
            <p><?php the_title(); ?><br /><?php echo get('Event-Venue'); ?><br /><?php echo get('Event-Date'); ?></p>
            </div>
        <?php endwhile; endif; wp_reset_query(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic