WordPress.org

Support

Support » Themes and Templates » Separate custom post type search results from blog search results

Separate custom post type search results from blog search results

  • Hi !
    I’m working on a new blog using the latest WordPress 3.0 RC, but I have a problem with the use of custom post types. I’ve asked on some French forums but I didn’t get any answer so I thought I should give this forum a try !

    What I did on my website is creating two distinctive parts : one for a podcast, the other for a blog. I’ve used custom post types for the podcast part, but what I would like to do now is about the search.

    When someone searches for something on this site, I want him to be redirected to the search.php page on which they will see two separate columns : one with the podcast entries, and the other one for the blog entries. I don’t want them to be mixed, because they don’t show up the same way (podcast entries have thumbnails and a flash player, blog entries have a date and a title only).

    I’ve tried some solutions but It keeps displaying both results… I’m no expert of php and I’ve searched everywhere without finding any solution. I hope my question was clear enough :/ Thanks !

Viewing 15 replies - 1 through 15 (of 17 total)
  • esmi

    @esmi

    Forum Moderator

    Purely theoretical but what about:

    <?php $podcasts = array();
    if (have_posts()) : while (have_posts()) : the_post();
    if ( is_post_type('podcast') ) {
        $podcasts[] = $post;
        continue;
    }
    else :?>
    [ usual Loop stuff ]
    <?php endwhile, endif;?>
    
    <?php
    global $post;
    foreach( podcasts as $podcast ) :
    setup_postdata($post);?>
    [ second Loop ]
    <?php endforeach; ?>

    [EDITED]

    Thanks, but it does not work :/ In fact it doesn’t show anything at all (blank screen)… I think it might be a syntax error ?

    else :>

    Shouldn’t there be something like a ; ?> here ?

    esmi

    @esmi

    Forum Moderator

    Sorry – yes. It should be else :?>

    Still blank screen with this code :

    <?php get_header(); ?>
    
    <?php $podcasts = array();
    if (have_posts()) : while (have_posts()) : the_post();
     if ( is_post_type('podcast') ) $podcasts[] = $post;
     else :?>
    
    	<?php the_title(); ?>
    
    <?php endwhile, endif;?>
    
    <?php
    global $post;
    foreach( podcasts as $podcast ) :
    setup_postdata($post);?>
    
    	<?php the_title(); ?>
    	<?php the_post_thumbnail( 'medium-post-thumbnail' ); ?>
    
    <?php endforeach; ?>
    
    <?php get_footer(); ?>

    I’ve just put the_title and the_post_thumbnail to see if there is any result showing up. But there isn’t. Have I made something wrong ?

    esmi

    @esmi

    Forum Moderator

    I can see something I’ve left out:

    if ( is_post_type('podcast') ) $podcasts[] = $post;
     else :?>

    should be:

    if ( is_post_type('podcast') ) {
        $podcasts[] = $post;
        continue;
    }
    else :?>

    Still doesn’t work 🙁

    esmi

    @esmi

    Forum Moderator

    What do you get? Anything?

    Anything again. Blank screen !

    esmi

    @esmi

    Forum Moderator

    OK- let’s backtrack a little. Try just using:

    <?php get_header(); ?>
    
    <?php $podcasts = array();
    if (have_posts()) : while (have_posts()) : the_post();
    if ( is_post_type('podcast') ) :
    $podcasts[] = $post;
    continue;
    else :?>
    
    <?php the_title(); ?>
    
    <?php endwhile, endif;?>
    
    <?php get_footer(); ?>

    and see if you can get a listing that excludes the podcast posts.

    Nope I’m sorry, still blank screen… Everything else but this search.php works.

    esmi

    @esmi

    Forum Moderator

    Did you register the post type “podcast”?

    http://codex.wordpress.org/Function_Reference/register_post_type

    Yes I did :

    register_post_type('podcast', array(
     'label' => __('Podcasts'),
     'singular_label' => __('Podcast'),
     'public' => true,
     'show_ui' => true,
     'capability_type' => 'post',
     'hierarchical' => false,
     'rewrite' => array("slug" => "podcast", "with_front" => false),
     'query_var' => false,
     'supports' => array('title', 'excerpt', 'editor', 'custom-fields', 'thumbnail', 'comments')
    ));

    And I already can read them (www.website.com/podcast/ and http://www.website.com/podcast/article -> for single page). Everything works fine :/

    esmi

    @esmi

    Forum Moderator

    So is_post_type('podcast') should work. Does:

    <?php get_header(); ?>
    
    <?php $podcasts = array();
    if (have_posts()) : while (have_posts()) : the_post();
    if ( is_post_type('podcast') ) :
    echo 'Boo';
    $podcasts[] = $post;
    continue;
    else :?>
    <?php the_title(); ?>
    <?php endwhile, endif;?>
    <?php get_footer(); ?>

    output anything?

    Nope. If I write

    <?php get_header(); ?>
    <p>Text</p>
    <?php get_footer(); ?>

    It displays the “Text”.

    Also, I have a

    <?php $loop = new WP_Query( array( 'post_type' => 'podcast', 'posts_per_page' => 10 ) ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    ...

    that is working on the site. So nothing wrong with the custom post type I guess.

    esmi

    @esmi

    Forum Moderator

    Try replacing if ( is_post_type('podcast') ) : with if ( $post->post_type == 'podcast' ) :

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Separate custom post type search results from blog search results’ is closed to new replies.