Title: [Plugin: Posts 2 Posts] exclude post type from search
Last modified: August 20, 2016

---

# [Plugin: Posts 2 Posts] exclude post type from search

 *  Resolved [Brian Cruikshank](https://wordpress.org/support/users/brianbws/)
 * (@brianbws)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/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/](http://wordpress.org/extend/plugins/posts-to-posts/)

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-exclude-post-type-from-search/#post-2956439)
 * It seems to me like you have some other code that sets ‘post_type’ => ‘any’ for
   searches. I would try to find it, rather than adding even more code to neutralize
   it.
 *  Thread Starter [Brian Cruikshank](https://wordpress.org/support/users/brianbws/)
 * (@brianbws)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-exclude-post-type-from-search/#post-2956442)
 * Thanks for the response.
 * I fixed the problem by setting _exclude\_from\_search => true_ on the post type.
 *  Thread Starter [Brian Cruikshank](https://wordpress.org/support/users/brianbws/)
 * (@brianbws)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-exclude-post-type-from-search/#post-2956443)
 * setting resolved

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.

 * ![](https://s.w.org/plugins/geopattern-icon/posts-to-posts_7a8e9d.svg)
 * [Posts 2 Posts](https://wordpress.org/plugins/posts-to-posts/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/posts-to-posts/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/posts-to-posts/)
 * [Active Topics](https://wordpress.org/support/plugin/posts-to-posts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/posts-to-posts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/posts-to-posts/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [Brian Cruikshank](https://wordpress.org/support/users/brianbws/)
 * Last activity: [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-exclude-post-type-from-search/#post-2956443)
 * Status: resolved