Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am working on a site with several post types - 4 to be exact - and I am trying to setup the search results page to have a filter/sort the results by post types.

For example, when a person searches a term they are taking to the page with the results, all posts found from all post types are shown but up top there are the different post types name links that will sort the results and show only the respective post type's post. Right now I have several loops on the search results page for each post type but when I test it, the loops are all showing the same results even though each loop has a query for a different post type.

How do I fix the loop so that it only shows the search results from that post type? I dont want to have different search forms or reset the query and end up losing the search term. Here is some of the code from the page -- http://pastebin.com/L9zEw1cn

The first loop is the default loop which will show all the results and the second loop is the loop I am trying to use for the rest of the post types.

So is there anyway to fix the second loop so that it only shows the search results for that post type?

Any help would be greatly appreciated. Thanks.

Reply

share|improve this question
1  
Your code is unreadable, there is no need to paste the entire template. Can you please post the code here, but only contain the necessary parts, like loops, without all your divs and styling? –  Mateusz Hajdziony Nov 17 '12 at 3:01
    
Ok sorry. The reason why I did that because I wasnt sure which code on the page was affecting the loops. This is a little above the first loop <?php global $wp_query; $total_results = $wp_query->found_posts; ?> This is the first loop <?php if(have_posts()) : ?> <?php while(have_posts()) : the_post() ?> //My divs// <?php endwhile; endif; ?> <?php wp_reset_postdata(); ?> –  mygm Nov 17 '12 at 5:51
    
Then the second loop which is the same for all the others except the post type name -- <?php $args = array( 'post_type' => 'videos', 's' => $s ); ?> <?php if(have_posts()) : ?> <?php while(have_posts()) : the_post() ?> //My divs <?php endwhile; endif; ?> <?php wp_reset_postdata(); ?> –  mygm Nov 17 '12 at 5:52
add comment

2 Answers

You can use filter posts_clauses For example:

add_filter( 'posts_clauses', 'post_query_order', 20, 1 );
function post_query_order( $pieces )
{
    global $wpdb;
    $pieces['orderby'] = $wpdb->prefix.'posts.post_type ASC';
    return $pieces;
}
share|improve this answer
add comment

In your second loop you are setting up arguments for posts query but you are not using them anywhere. You should place query_posts( $args ) before the second loop. You could also use a get_posts function or WP_Query class.

EDIT: From page about query_posts function on WordPress codex:

query_posts() is the easiest, but not preferred or most efficient, way to alter the default query that WordPress uses to display posts. Use query_posts() to display different posts than those that would normally show up at a specific URL.

Right now, in your code, you are looping twice through the same posts. The second loop is exactly the same as the first one.

Now, if you place query_posts() with different arguments before the second loop, like this:

$args = array( 'post_type' => 'videos', 's' => $s );
query_posts( $args );
if(have_posts()) :
while(have_posts()) :
the_post();

// Display the post

endwhile; endif;
wp_reset_postdata(); ?>

You will be looping through different set of posts.

share|improve this answer
    
I dont understand, how do I place query posts before the loop? –  mygm Nov 17 '12 at 19:07
    
@mygm Please check the edit. –  Mateusz Hajdziony Nov 17 '12 at 20:07
    
I tried using the code you provided and it's not showing any posts at all now. It shows posts with the regular loop but not like I need it to. What should I do? –  mygm Nov 18 '12 at 5:54
    
@mygm Just let me see the code after placing my snippet. –  Mateusz Hajdziony Nov 18 '12 at 5:57
    
Ok this is what I have opening the second loop - <?php $args = array( 'post_type' => 'video', 's' => $s ); query_posts( $args ); if(have_posts()) : while(have_posts()) : the_post(); ?> Then I have my divs and everything else inbetween and this is how it closes <?php endwhile; endif; wp_reset_postdata(); ?> Im not sure but I think maybe there is something else on the page possibly effecting the loop? Is there any way I can show the full template code? Oh also I change "videos" to "video" just to see if maybe I made a mistake but it did not work either way. –  mygm Nov 18 '12 at 6:26
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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