WordPress.org

Ready to get started?Download WordPress

Forums

Separate custom post type search results from blog search results (18 posts)

  1. abiwab
    Member
    Posted 4 years ago #

    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 !

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    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]

  3. abiwab
    Member
    Posted 4 years ago #

    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 ?

  4. esmi
    Forum Moderator
    Posted 4 years ago #

    Sorry - yes. It should be else :?>

  5. abiwab
    Member
    Posted 4 years ago #

    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 ?

  6. esmi
    Forum Moderator
    Posted 4 years ago #

    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 :?>
  7. abiwab
    Member
    Posted 4 years ago #

    Still doesn't work :(

  8. esmi
    Forum Moderator
    Posted 4 years ago #

    What do you get? Anything?

  9. abiwab
    Member
    Posted 4 years ago #

    Anything again. Blank screen !

  10. esmi
    Forum Moderator
    Posted 4 years ago #

    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.

  11. abiwab
    Member
    Posted 4 years ago #

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

  12. esmi
    Forum Moderator
    Posted 4 years ago #

    Did you register the post type "podcast"?

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

  13. abiwab
    Member
    Posted 4 years ago #

    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 :/

  14. esmi
    Forum Moderator
    Posted 4 years ago #

    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?

  15. abiwab
    Member
    Posted 4 years ago #

    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.

  16. esmi
    Forum Moderator
    Posted 4 years ago #

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

  17. abiwab
    Member
    Posted 4 years ago #

    Sorry for the delay. Tried your code, doesn't work.
    But hey, I founded this topic : http://wordpress.org/support/topic/346317 .
    Maybe it could help ?

  18. esmi
    Forum Moderator
    Posted 4 years ago #

    And I think I've found the issue: is_post_type has been dropped in favour of post_type_exists() !

    However you should still be able to use if( get_post_type() == 'podcast' )

Topic Closed

This topic has been closed to new replies.

About this Topic