Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

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(); ?>
share|improve this question
    
search for questions using add_rewrite_endpoint, pretty sure there's one on this site detailing author endpoints. –  Milo Nov 14 '13 at 5:57

1 Answer 1

up vote 1 down vote accepted

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 );
}
share|improve this answer
    
Excelent answer my friend!! But my pagination doesnt work when i go to author/admin/liked-posts/page/2/ , author/admin/liked-posts/page/3/ and so on... Is there and extra rule for that? –  Oscar Nov 14 '13 at 17:45
    
nvm i just did it! -> $author_rules['author/([^/]+)/liked-posts/?$'] = 'index.php?author_name=$matches[1]'; $author_rules['author/([^/]+)/liked-posts/page/?([0-9]{1,})/?$'] = 'index.php?author_name=$matches[1]&paged=$matches[2]'; return $author_rules; –  Oscar Nov 14 '13 at 18:00
    
Thank you so much Milo!! –  Oscar Nov 14 '13 at 18:00
    
see edit above for pagination without adding a rewrite rule. –  Milo Nov 14 '13 at 18:09
    
Milo i added if( strpos( $wp_query->query_vars['liked-posts'] ,'page') !== false ) { echo substr( $wp_query->query_vars['liked-posts'], 5 ); } into my functions.php and my pagination still not working :/ –  Oscar Nov 15 '13 at 0:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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