2

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

3
  • 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? Commented Nov 17, 2012 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
    Commented Nov 17, 2012 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
    Commented Nov 17, 2012 at 5:52

2 Answers 2

1

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;
}
0

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.

6
  • I dont understand, how do I place query posts before the loop?
    – mygm
    Commented Nov 17, 2012 at 19:07
  • @mygm Please check the edit. Commented Nov 17, 2012 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
    Commented Nov 18, 2012 at 5:54
  • @mygm Just let me see the code after placing my snippet. Commented Nov 18, 2012 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
    Commented Nov 18, 2012 at 6:26

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.