Posts 2 Posts
[resolved] exclude post type from search (4 posts)

  1. Brian Cruikshank
    Posted 3 years ago #

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

    p2p_type( 'post_to_author' )->each_connected( $wp_query );
    while ( have_posts() ) : the_post(); ?>
    <?php the_title(); ?>
    <p class="author">
    	<span class="sep">By </span>
    	$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>';
    	if (count($authors) > 1) {
    		$lastauthor = array_pop($authors);
    		echo implode(', ', $authors) . ' and ' . $lastauthor;
    		echo $authors[0];
    <?php wp_reset_postdata(); ?>
    <p><?php the_excerpt(); ?></p>
    <?php endwhile; ?>


  2. scribu
    Plugin Author

    Posted 3 years ago #

    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.

  3. Brian Cruikshank
    Posted 3 years ago #

    Thanks for the response.

    I fixed the problem by setting exclude_from_search => true on the post type.

  4. Brian Cruikshank
    Posted 3 years ago #

    setting resolved

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Posts 2 Posts
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic