-1

A normal author.php template has a single loop that displays a list of all the author's posts.

I'd like another loop place just above this normal post loop that lists all author's (defined) CPT posts.

In effect it would look like this

author profile info

author CPT posts

author normal posts

  • pre_get_posts, and then rewind_posts() in the loop – Pieter Goosen Sep 23 '15 at 16:27
  • Thanks, unforntunately I know nothing very little about getting this to work. Any chance in putting it in a basic working example? – Pete Sep 25 '15 at 11:25
4

As I have stated in comments, you would want to use pre_get_posts to add your custom post type to your author archive pages. In addition, once that is done, it is easy to sort the posts, you simply need to run the main loop twice, once only sending the custom post type posts to screen and the second time only post posts.

Here is an example: (NOTE: This is all untested, might be buggy, but this should get you very close, if not completely there. Also, PHP 5.4+ is required)

add_action( 'pre_get_posts', function ( $q )
{
    if (    !is_admin() // Run this only on front end queries
         && $q->is_main_query() // Only run this on the main query
         && $q->is_author() // Run this only on author archive pages
    ) {
        $q->set( 'post_type', ['post', 'custom_post_type_name_here'] );
    }
});

Then in your template

if ( have_posts() ) {
    while ( have_posts() ) {
    the_post();

        if ( $post->post_type == 'custom_post_type_name' ) {
            // Output posts from your custom post type
        }
    } // endwhile

    rewind_posts(); // This will reweind posts to the first post so we can rerun the loop

    while ( have_posts() ) {
    the_post();

        if ( $post->post_type == 'post' ) {
            // Output posts from the defualt post post type
        }
    } // endwhile
} // endif

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.