0

I would like to do custom search pages that allow users to see posts written by one and only one specific author.

For example, let Bob and Bill be the authors on a blog. One page would allow me to see all of Bob's posts by default and to search within, and another would allow me to do the same with Bill's.

The following code allows me to see all posts, but I want to customize it to filter by author and to add a search function. Anyone can suggest an easy fix?

$args=array(
    'post_type' => 'post', 
    'post_status' => 'publish', 
    'posts_per_page' => -1, 
    'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) { 
    echo 'List of Posts'; 
    while ($my_query->have_posts()) : $my_query->the_post(); ?> 
        <p>
            <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
                <?php the_title(); ?>
            </a>
        </p> 
        <?php the_excerpt(); 
    endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().

1 Answer 1

0

Jus add the parameter 'author__in'=> array( 2, 3, 5 ) to $args and pass the author ID or IDs. You'll get the results.

$args = array(
    'author__in'=> array( 2, 3, 5 ), // array of authors IDs you like to include
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'caller_get_posts'=> 1
);

And I don't think you need the $my_query = null; to nullify the variable before declaring it.

1
  • Hello @user105460 , does my code helped ? Or do you have any other query ?
    – CodeMascot
    Oct 24, 2016 at 3:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.