0

I am trying to create an author.php page that calls custom post types by the author appearing on that page. My problem is that when using the code below -- which filters for the post type 'video' -- the code outputs all custom post types, not just 'video'. The caveat is when I remove the array's author key and variable 'author' => $author which then has the effect of allowing the code to output the desired custom post type 'video' but from every author. Any ideas of how to fix this?

Code used:

<?php
    $args = array(
        'post_type'      => array( 'audio' ),
        'posts_per_page' => 10,
        'author'         => $author
    );


    $author_videos = new WP_Query( $args );

    if ( $author_videos->have_posts() ) : while ( $author_videos->have_posts() ) : $author_videos->the_post(); ?>
            <p><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></p>       
        <?php endwhile;
            wp_reset_postdata();
endif; ?>
0

Have you read the codex page Author Templates? There it states:

when a viewer clicks on a link to a post author, by default he or she is taken to a page listing the posts from that particular author in chronological order

This depends on having the correct links setup, which can be done with the_author_posts_link() or wp_list_authors(). A custom query shouldn't be necessary, you can alter the post types by hooking into pre_get_posts.

Code:

function wpse124293_cpts_for_author_archives( $query ) {
    if ( ! $query->is_main_query() || is_admin() )
        return;
    if ( is_author() ) {
        //only show cpt audio
        $query->set( 'post_type', array( 'audio' ) );
        //to show builtin post and cpt audio use: array( 'post', 'audio' )
    }
}
add_action( 'pre_get_posts', 'wpse124293_cpts_for_author_archives' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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