How to set up Author archives with sub category URL

So i have a site where i have used the authors.php page so that i can display individual user’s profile pages.. the url then looks something like:
http://mydomain.com/author/test/

What i would like to do is have it so that I can add subpages to this that are common for each user ie:
http://mydomain.com/author/test/liked-posts/ where i can add all the posts that the user has liked ie: liked-posts.php

Read More

is there a place i should be telling URL’s of this structure to be redirecting to a specific file? and how would i do that?

This is the complete code that im using to query my posts:

<?php
/**
 * Template Name: Posts liked by Author
 *
 * for a child theme of Twenty_Twelve
 */

get_header(); ?>

    <div id="primary" class="site-content">

        <div id="content" role="main">

        <?php
$author = get_user_by( 'slug', get_query_var( 'author_name' ) );
$post_ids = $wpdb->get_col( "SELECT DISTINCT post_id FROM {$wpdb->prefix}wti_like_post WHERE user_id = {$author->ID}" );
if( strpos( $wp_query->query_vars['liked-posts'] ,'page') !== false ) {
    $paged = substr( $wp_query->query_vars['liked-posts'], 5 );
}

$args = array(
    'posts_per_page' => 4,
    'paged' => $paged,
    'order' => 'ASC',
    'post__in' => $post_ids
);

$wp_query = new WP_Query();
$wp_query->query( $args ); ? >

            <?php if( have_posts() ) : ?>

            <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

                <?php get_template_part( 'content', get_post_format() ); ?>
                <?php comments_template( '', true ); ?>
            <?php endwhile; // end of the loop. ?>

   <?php twentytwelve_content_nav( 'nav-below' ); ?> 



            <?php else : ?>
            <article id="post-0" class="post no-results not-found">
                <header class="entry-header">
                    <h1 class="entry-title"><?php _e( 'Nothing Found', 'twentytwelve' ); ?></h1>
                </header>

                <div class="entry-content">
                    <p><?php _e( 'Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve' ); ?></p>
                    <?php get_search_form(); ?>
                </div><!-- .entry-content -->
            </article><!-- #post-0 -->

            <?php endif; wp_reset_postdata(); ?>


        </div><!-- #content -->
    </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Related posts

1 comment

  1. As I mentioned in my comment, you can use add_rewrite_endpoint to accomplish this.

    First, add the endpoint:

    function wpa_author_endpoints(){
        add_rewrite_endpoint( 'liked-posts', EP_AUTHORS );
    }
    add_action( 'init', 'wpa_author_endpoints' );
    

    After flushing rewrite rules (visit your Settings > Permalinks page in admin), author URLs can now be appended with /liked-posts/.

    Next, add a filter to author_template to load a different template for these requests. This checks if the request has set the liked-posts query var, and loads the template liked-posts.php if it exists:

    function wpa_author_template( $template = '' ){
        global $wp_query;
        if( array_key_exists( 'liked-posts', $wp_query->query_vars ) )
            $template = locate_template( array( 'liked-posts.php', $template ), false );
        return $template;
    }
    add_filter( 'author_template', 'wpa_author_template' );
    

    Within that template, you can use get_queried_object to fetch the author data for the queried author, which you can use in additional queries to load your author data.

    EDIT – pagination doesn’t work with an endpoint, because anything after the endpoint gets put into the endpoint query var. so to get the page number, just extract it from the query var:

    if( strpos( $wp_query->query_vars['liked-posts'] ,'page') !== false ) {
        echo substr( $wp_query->query_vars['liked-posts'], 5 );
    }
    

Comments are closed.