Support » Plugin: Posts 2 Posts » [Plugin: Posts 2 Posts] exclude post type from search

  • I have a custom post type ‘author’ which I connect to all posts.

    I’ve got everything working, but I’m having trouble with search. I’d like to exclude the author post type from search and just use the regular ‘post’ posts type. The problem is if I do something like this so search only shows posts, I can’t get the connected author names to show up:

    function exclude_authors_from_search ( $query ) {
    	if ( $query->is_search && !is_admin() )
    		$query->set( 'post_type','post' );
    }
    add_action('pre_get_posts', 'exclude_authors_from_search');

    But if I don’t do that, I get a giantic mess of authors. How can I exclude authors without excluding them as connected posts? Here is my code (more or less):

    <?php
    p2p_type( 'post_to_author' )->each_connected( $wp_query );
    while ( have_posts() ) : the_post(); ?>
    <article>
    <?php the_title(); ?>
    <p class="author">
    	<span class="sep">By </span>
    	<?php
    	$authors = array(); // multi-authors possible
    	foreach ( $post->connected as $post ) : setup_postdata( $post );
    		$authors[] = '<a href="'.get_permalink().'" title="'. get_the_title() .'">'. get_the_title() .'</a>';
    	endforeach;
    	if (count($authors) > 1) {
    		$lastauthor = array_pop($authors);
    		echo implode(', ', $authors) . ' and ' . $lastauthor;
    	}
    	else
    		echo $authors[0];
    	?>
    </p>
    <?php wp_reset_postdata(); ?>
    <p><?php the_excerpt(); ?></p>
    </article>
    <?php endwhile; ?>

    http://wordpress.org/extend/plugins/posts-to-posts/

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Plugin: Posts 2 Posts] exclude post type from search’ is closed to new replies.