0

The authorpage is begin generated, and every post that an author has made will be shown. However I only want to show posts from a specific category (id=1).

So basically, when you visit

mysite.com/author/John

you should see only posts from John, with category ID=1 and on

mysite.com/author/Bob

you should see only posts from Bob, with category ID=1.

When I do this in archive.php:

/* Start the Loop */ 
query_posts('cat=1');                               
while (have_posts()) {
    the_post();
    get_template_part('content', get_post_format());
}

Unfortunately, when I do this, every post in category 1, will be shown on every author page...(You see Bobs and Johns posts on Johns author-page).

I have also tried this, but same results as above:

$curauth = get_userdata($row->ID);
$user_link = get_author_posts_url($curauth->ID);
$posts = get_posts(array(
     'author' => $curauth->ID,
     'cat' => 1
     'posts_per_page' => -1,
    )
);
$post_count = count($posts);

I have 2 questions.

  1. Should I code in archive.php or should I make the adjustments in author.php (not available in the theme).

  2. More importantly, how do I show catID=1 for every author ?

PS: View full code here

1
  • 1
    Welcome to WPSE. Just one tip, properly format your code before posting, it makes it easier for others to read and help you :-) Feel free to take a tour and also read through How to Ask. Also, we have a one question per post policy :-) Commented Oct 29, 2014 at 17:06

1 Answer 1

0

I have written a quite extensive post on a similar subject that you can read here. Also take your time and read all linked posts.

In there I have explained why you should not be using query_posts and why you should not use custom queries in place of the main query

A few things here:

  • Remember to create a child theme, do not make changes to a theme that you have not written. You will loose all your work if you update that theme

  • It is real easy to create your own author archive page. Simply make a copy of your index.php and rename it author.php. It is that easy

  • Use pre_get_posts to make changes to your author page. Do not make changes directly through a custom query on your author page. You can use the following in your functions.php

    function wpse166864_include_category( $query ) {
        if ( !is_admin() && $query->is_author() && $query->is_main_query() ) {
           $query->set( 'cat', '1' );
        }
    }
    add_action( 'pre_get_posts', 'wpse166864_include_category' );
    
2
  • Thank you so much, the code for function.php simply worked. I'll make sure to read the links provided. Also I have a question, just to be sure, what does "wpse166864" mean ? It's just randomly chosen for 'add_action' right ? Commented Oct 29, 2014 at 18:17
  • Great to hear it worked. On the naming convention. It is just to give the function a unique name. It simply means [w]ord[p]ress [s]tack[e]xhange post number [166864] Commented Oct 29, 2014 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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