WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Brian Cruikshank
    Member
    Posted 1 year 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):

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

  2. scribu
    Member
    Plugin Author

    Posted 1 year 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
    Member
    Posted 1 year ago #

    Thanks for the response.

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

  4. Brian Cruikshank
    Member
    Posted 1 year ago #

    setting resolved

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic