3

I have created wordpress template that uses two loops via WP_Query object. Everything works fine except Search widget. I've been pulling my hairs out for three days now Googling like a madman,but didn't find a solution. For example in my searchbox I type: First cat (that is the name and content of my post inside "cat" category) and click search and NOTHING happens.

Strange thing is this: if I create a "normal" (main Loop) above other two Loops the Search widget WORKS, but the problem is that now my "normal" (main Loop) is showing ALL posts from my other two loops.
Can somebody please help me or give a suggestion how to solve this?
Link to my site if you want to check my non working search widget:(
Index.php look's like this:

 <?php get_header(); ?>
<?php get_sidebar('Main Sidebar'); ?>   
<div id="blogwrapper">  
<div id="blog">  
    <?php if (have_posts()) : ?>        
    <?php                             
    $paged1 = isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
    $paged2 = isset( $_GET['paged2'] ) ? (int) $_GET['paged2'] : 1;
    $args=array(

    'category_name' => 'cats',
    'paged' => $paged1,
    'posts_per_page' => 2,
    'order' => 'DESC'
     );
     $temp = $wp_query;
     $wp_query= null;
     $wp_query = new WP_Query($args);
     while ( $wp_query->have_posts() ) : $wp_query->the_post();
     ?>     
    <div class="post">  
    <div class="post_title">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    </div>
        <div class="entry"> 

        <!--< ?php the_meta(); ?>-->

            <?php the_post_thumbnail(); ?>
            <?php the_content('Read on...'); ?>

            <p class="postmetadata">
            <?php _e('Filed under&#58;'); ?> <br/><?php the_category(', ') ?> <?php _e('by'); ?> <?php  the_author(); ?><br />
            <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?> <?php edit_post_link('Edit', ' &#124; ', ''); ?>
            </p>
        </div>          
    </div>
    <?php endwhile;?>
    <?php else : ?>
    <h2>Not Found</h2>
    <p>Sorry, but you are looking for something that isn't here.</p>
    <?php endif; ?>
    <div class="navigation">
    <?php
     $pag_args1 = array(
        'format'  => '?paged1=%#%',
        'current' => $paged1,       
        'total'   => $wp_query->max_num_pages,      
        'add_args' => array( 'paged2' => $paged2 )
    );
    echo paginate_links( $pag_args1 );
    ?>
    </div>      
    <?php
$wp_query = null;
$wp_query = $temp;
wp_reset_postdata();
?>
    </div><!--/blog-->          
    <div id="blogs">    
    <?php            
$args2=array(

'category_name' => 'dogs',
'paged' => $paged2,
'posts_per_page' => 2
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args2);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<div class="post">      
    <div class="post_title">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    </div>
        <div class="entry"> 
            <?php the_post_thumbnail(); ?>
            <?php the_content('Read on...'); ?>
            <p class="postmetadata">
            <?php _e('Filed under&#58;'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php  the_author(); ?><br />
            <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?> <?php edit_post_link('Edit', ' &#124; ', ''); ?>
            </p>
        </div>          
    </div>
    <?php endwhile;?>       
    <div class="navigation">        
    <?php
       $pag_args2 = array(
        'format'  => '?paged2=%#%',
        'current' => $paged2,
        'total'   => $wp_query->max_num_pages,
        'add_args' => array( 'paged1' => $paged1 )
    );
    echo paginate_links( $pag_args2 );
    ?>
    </div>      
    <?php
$wp_query = null;
$wp_query = $temp;
wp_reset_postdata();
?>              
    </div>
     </div><!--blogswrapper-->
   <?php get_footer(); ?>

Modified Index.php with Main loop that kinda works (Main loop showing all the post's from other two loops,but search widget WORKS..)

 <?php get_header(); ?>
<?php get_sidebar('Main Sidebar'); ?>   
<div id="blogwrapper"> 
    <div id="blog">
       //Now Search Widget is WORKING but its's showing posts from 
       //other TWO LOOPS so it looks like a BIG mess:( 
    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>

    <div class="post">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

        <div class="entry"> 
            <?php the_post_thumbnail(); ?>
            <?php the_content(); ?>

            <p class="postmetadata">
            <?php _e('Filed under&#58;'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php  the_author(); ?><br />
            <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?> <?php edit_post_link('Edit', ' &#124; ', ''); ?>
            </p>

        </div>
    </div>

    <?php endwhile; ?>

    <div class="navigation">
    <?php posts_nav_link(); ?>
    </div>

    <?php endif; ?>
</div>  
<div id="blog">  
    <?php if (have_posts()) : ?>        
    <?php                             
    $paged1 = isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
    $paged2 = isset( $_GET['paged2'] ) ? (int) $_GET['paged2'] : 1;
    $args=array(

    'category_name' => 'cats',
    'paged' => $paged1,
    'posts_per_page' => 2,
    'order' => 'DESC'
     );
     $temp = $wp_query;
     $wp_query= null;
     $wp_query = new WP_Query($args);
     while ( $wp_query->have_posts() ) : $wp_query->the_post();
     ?>     
    <div class="post">  
    <div class="post_title">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    </div>
        <div class="entry"> 

        <!--< ?php the_meta(); ?>-->

            <?php the_post_thumbnail(); ?>
            <?php the_content('Read on...'); ?>

            <p class="postmetadata">
            <?php _e('Filed under&#58;'); ?> <br/><?php the_category(', ') ?> <?php _e('by'); ?> <?php  the_author(); ?><br />
            <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?> <?php edit_post_link('Edit', ' &#124; ', ''); ?>
            </p>
        </div>          
    </div>
    <?php endwhile;?>
    <?php else : ?>
    <h2>Not Found</h2>
    <p>Sorry, but you are looking for something that isn't here.</p>
    <?php endif; ?>
    <div class="navigation">
    <?php
     $pag_args1 = array(
        'format'  => '?paged1=%#%',
        'current' => $paged1,       
        'total'   => $wp_query->max_num_pages,      
        'add_args' => array( 'paged2' => $paged2 )
    );
    echo paginate_links( $pag_args1 );
    ?>
    </div>      
    <?php
$wp_query = null;
$wp_query = $temp;
wp_reset_postdata();
?>
    </div><!--/blog-->          
    <div id="blogs">    
    <?php            
$args2=array(

'category_name' => 'dogs',
'paged' => $paged2,
'posts_per_page' => 2
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args2);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<div class="post">      
    <div class="post_title">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    </div>
        <div class="entry"> 
            <?php the_post_thumbnail(); ?>
            <?php the_content('Read on...'); ?>
            <p class="postmetadata">
            <?php _e('Filed under&#58;'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php  the_author(); ?><br />
            <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?> <?php edit_post_link('Edit', ' &#124; ', ''); ?>
            </p>
        </div>          
    </div>
    <?php endwhile;?>       
    <div class="navigation">        
    <?php
       $pag_args2 = array(
        'format'  => '?paged2=%#%',
        'current' => $paged2,
        'total'   => $wp_query->max_num_pages,
        'add_args' => array( 'paged1' => $paged1 )
    );
    echo paginate_links( $pag_args2 );
    ?>
    </div>      
    <?php
$wp_query = null;
$wp_query = $temp;
wp_reset_postdata();
?>              
    </div>
     </div><!--blogswrapper-->
   <?php get_footer(); ?>
4
  • Did you read: When should you use WP_Query vs query_posts() vs get_posts()?
    – brasofilo
    Commented Jan 24, 2013 at 20:26
  • Thx for the tip but I an not great with PHP, but I DID tried to solve my problem using WP_Query and even query_posts()...but as I was saying not that good with PHP. If you have suggestions for solving this that would be great... THX again
    – Dejo Dekic
    Commented Jan 24, 2013 at 20:40
  • Loops and pagination are not my area. Just wanted to make sure query_posts was not the culprit.
    – brasofilo
    Commented Jan 24, 2013 at 20:42
  • 1
    Deleted my question from Stack Overflow...
    – Dejo Dekic
    Commented Jan 24, 2013 at 20:58

1 Answer 1

0

Somebody shoot me please... Since this is the first theme I developed I didn't know that I needed a search.php for my form to work! Anyway I created search.php and inserted The Loop, after I did that I've copied that file inside my theme and my search form worked like a charm!!
Hope this will help somebody else :)
My search.php looks like this:

<?php
/**
* The template for displaying Search Results pages.
*
* 
* 
* 
*/
    get_header(); ?>
<div id="blog">
    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    <div class="post">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <div class="entry"> 
            <?php the_post_thumbnail(); ?>
            <?php the_content(); ?>
            <p class="postmetadata">
            <?php _e('Filed under&#58;'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php  the_author(); ?><br />
            <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?> <?php edit_post_link('Edit', ' &#124; ', ''); ?>
            </p>
        </div>
    </div>
    <?php endwhile; ?>
    <div class="navigation">
    <?php posts_nav_link(); ?>
    </div>
    <?php endif; ?>
    </div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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