• Resolved pk-71

    (@pauljohnknight)


    Hi

    I’m building a theme and I’m trying to have it so that when a user clicks on the author name it only shows the custom post type for that author, and if the user is on the blog page and clicks and author name it only shows the blog posts by that author.

    I’ve come across this piece of code:

    function wpse107459_add_cpt_author( $query ) {
        if ( !is_admin() && $query->is_author() && $query->is_main_query() ) {
            $query->set( 'post_type', array('post', 'YOUR_CUSTOM_POST_TYPE' ) );
        }
    }
    add_action( 'pre_get_posts', 'wpse107459_add_cpt_author' );

    This almost solves the problem, but what this does is combine the blog posts and custom post types together. I would like it so the CPTs and blog post remain separate. If I delete the ‘post’ parameter on the 3rd line, this removes blog posts from the CPT archive when I click the author name, but it also removes them when I click the author name under the blog page.

    What I need is some type of function called is_post('custom-post-type-name') that I can use in a piece of conditional logic, but I can’t find anything like this in the codex?

    I’d be happy to have any solution either in functions.php or in the author.php file.

    Any help would be great.

    Paul

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    It’s not possible to determine where the request came from using any part of the current query. I suppose you could check $_SERVER[‘HTTP_REFERER’] and decide what post type to use in the query based on the value. It’s a viable approach if this theme is for your own site. If it’s for public distribution, it’s not a reliable method because URLs for each situation are variable.

    I think it’s better to specify the desired post type when outputting the author archive link in each situation. You would need a different template for the CPT archive and blog archive. Or conditionally vary the URL by the post_type query var taken from the current query (this is a different current query than the one mentioned in my first paragraph — blog archive vs. author archive). The usual author archive URL is something like example.com/author/my-login/. Depending on the post type being listed, change the URL to something like example.com/author/my-login/?post_type=my_custom_post_type.

    If I’m not mistaken, that alone should result in the list you want without the need to alter the query through pre_get_posts.

    Thread Starter pk-71

    (@pauljohnknight)

    Hi @bcworkz,

    At the moment I do have separate template files for the CPT archive and blog archive, but I don’t understand how I’m supposed to conditionally vary the archive depending on whether the user clicks on the author name in the blog (and thus go to a blog archive) and when the user clicks on the author name in a CPT (and thus send them to a CPT archive).

    I can’t seem to find any information on how to achieve this. I’ve included my loop for the archive-cpt.php file below (in this case the file is called archive-job.php). Are you saying i need to apply some conditional logic to where it outputs <?php the_author_posts_link(); ?>

    I would have thought there would be a parameter I could add into the above template tag that was the name of the custom-post-type or something. This is where I’m stuck: in terms of trying to force it to only show CPTs. The blog archive page I can keep as it is, because that by default does only show blog posts. My problem obviously is the template tag <?php the_author_posts_link(); ?> also only shows blog posts when used in the archive-job.php file.

    My loop in the archive-job.php template file is:

    <?php
    	while(have_posts()){
    	the_post(); ?>
    
    	<div class="row">
    
    		<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    		<div class="metabox">
    			<p>Posted by <?php the_author_posts_link(); ?> on, <?php the_time('jS M Y'); ?> | <?php echo get_the_term_list ($post->ID, 'job_type', 'Job Type: ', ', ', ''); ?></p>
    		</div>
    
    		<div class="generic-content">
    			<?php the_excerpt(); ?>
    			<a class="btn btn-left" href="<?php the_permalink(); ?>">Continue reading...</a>
    		</div>
    
    	</div>
    
    <?php } ?>
    Moderator bcworkz

    (@bcworkz)

    You’re right, it all hinges on what is currently the_author_posts_link();. It’s convenient you have different templates for each post type, it saves us some extra coding. The current author link function is too simplistic for our new need. Replace it with all of the following (maintain the <?php ?> delimiters):

    global $authordata;
    $link = get_author_posts_url( $authordata->ID, $authordata->user_nicename );
    $link = sprintf( '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
       esc_url( add_query_arg('post_type', 'template_CPT', $link )),
       esc_attr( sprintf( 'Posts by %s', get_the_author())),
       get_the_author()
    );
    echo $link;

    On each template, replace “template_CPT” in the 4th line above with the actual post type for the respective template. Following the resulting link will result in an author archive page listing only the specified post type.

    This code is essentially the internal guts of the_author_posts_link() function, modified to add the post type query argument.

    Thread Starter pk-71

    (@pauljohnknight)

    Wow that’s amazing. Thank you so much @bcworkz!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Show Custom Post Type Archive by Author’ is closed to new replies.