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.

How can i display 3 post types in same page search results.

http://cinema.trancelevel.com/

For example i have

post_type1-Movies post_type2-Trailers post_type3-Subtitrari

Wen i search i want to display the search results..

Ex: i Search for Battleship Movie.

I want the search results to display somting like this

1.)The movie: Battleship

2.)The Trailers: - Battleship HD - Battleship Trailer Oficial

3.)The News All the news that contain the word Battleship.

Or.. how can i display for each post_type diferent themplate...

EX: For Movies it show fine:

-http://cinema.trancelevel.com/?s=Battleship&post_type=post

For Trailers it show the teme of movies:

-http://cinema.trancelevel.com/?s=Battleship&post_type=trailers

Wath i use in Search

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<input class="search_input" type="text" name="s" id="s" value="Cauta filme, subtitrari, trailere, stiri" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"/>
    <select name="post_type" class="search_select">
        <option value="post">Alege:</option>
        <option value="post">Filme</option>
        <option value="persoane">Actori</option>
        <option value="trailers">Trailere</option>
    </select>
    <input type="submit" id="searchsubmit" class="search_buton" value="Cauta" />
</form>  
share|improve this question

3 Answers 3

up vote 2 down vote accepted

Make sure that your registration arguments for your post types have the public argument set to true or, alternately, publicly_queryable set to true and exclude_from_search set to false.

Then, modify your search query by hooking into the pre_get_posts action:

function filter_search( $query ) {
    $postTypes = array( 'post', 'post_type1-Movies', 'post_type2-Trailers', 'post_type3-Subtitrari' );

    //Here you can modify the $postTypes array, say to account for the value of $_REQUEST['post_type'] as set by your dropdown or some such.

    if ( $query->is_search ) {
        $query->set( 'post_type', $postTypes );
    }
    return $query;
}
add_action( 'pre_get_posts', 'filter_search' );

Now all of your custom post types will be included in search queries.

If you wish to group multiple post types relating to the same subject, however (i.e. a specific movie or some such), it may be wiser to only search the post-type of the primary subject (i.e. searching for "Battleship" would only search your movie post-type). Associating other post types with the subject could be done by storing the primary subject's id in the meta-data for each associated item. This would allow you to query each of the other post types for associated data by simply searching for the primary subject's id, ideally when displaying it from within the loop (i.e. when looping through the results of the "Battleship" search, query the trailers post type for items with a meta field, "movie" set to the ID of the movie post currently being processed).

share|improve this answer

You would need to alter the wp_query on your search page to accept 'post_type' => $_get['post_type'] - this will grab your dropdown selection.

Check out Creating a Search Page in the codex for how to create/customize a search results page, and the WP_Query class for how to create a custom query.

share|improve this answer
    
Please use the WYSIWG editor for code formatting. Thanks. –  kaiser Sep 5 '12 at 23:52
    
can you paste the code here? –  Batman Sep 6 '12 at 12:40

There are a few ways this could be done. I've thought about doing this for a project of mine.

  1. If you want to have the different post types grouped together then have an initial loop that separates the posts by post type into different arrays, then use a different template for each array.
  2. If you want list the results all together but want to denote which is which then in your loop you check for post type then use the appropriate template via the get_template_part function
share|improve this answer
    
hy Manny Fleurmod thanks for reply, can you explayn more please... i do not undestand... i make point 1 it work but plese see the link now, but i want to search to all custom field same time and retrive in same page , Movie, Trailers, news... that contain the Word Battleship... –  Batman Sep 7 '12 at 21:52

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.