WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] How do I….list categories an author has wrote in on author.php

[Resolved] How do I….list categories an author has wrote in on author.php

  • scenario: a site with multiple authors. I click into a page about this author, and i want to see a list of categories this author has wrote in.

    how do i do this? im pretty stumped here. also how do i link to those categories?

Viewing 15 replies - 1 through 15 (of 34 total)
  • Since you are using Author Templates you just need to create an array of all the categories from get_the_category(), as you loop through a given author’s posts.

    Related:
    http://php.net/manual/en/language.types.array.php
    Function_Reference/get_the_category

    i still cant seem to come up with a way to make this work=\

    Well at least start here: http://wordpress.org/support/topic/327359

    @michaelh
    sorry about the duplicate i couldnt find the older one, thank you for closing it.

    =======================================================================

    Hello!
    my website: ampersandbox.net/?author=2

    Basically on every author’s page I would like to display a list on the right side of categories the current author has contributed to (has written a post in)

    I’m not sure how to go about this but I started trying to write some code I came up with this code but for some reason it only displays one category which happens to be category 2. (check column on right labeled CATEGORIES)

    <?php
    //Gets category and author info
      global $wp_query;
    $cats = get_the_category();
      $postAuthor = $wp_query->post->post_author;
    $tempQuery = $wp_query;
    // related category posts
      $catlist = "";
      forEach( $cats as $c ) {
      if( $catlist != "" ) { $catlist .= ","; }
      $catlist .= $c->name;
      $catlist .= $c->description;
      }
      $newQuery = "posts_per_page=5&cat=" . $catlist;
      query_posts( $newQuery );
    $categoryPosts = "";
      $count = 0;
      ?>
    <?php echo $catname .' '. $catlist; ?>

    still wasnt able to achieve this not much of a php coder =\

    <?php
    //get all posts for an author, then collect all categories
    //for those posts, then display those categories
    $cat_array = array();
    $args=array(
     'author' => 4,
     'showposts'=>-1,
     'caller_get_posts'=>1
    );
    $author_posts = get_posts($args);
    if( $author_posts ) {
      foreach ($author_posts as $author_post ) {
        foreach(get_the_category($author_post->ID) as $category) {
          $cat_array[$category->term_id] =  $category->term_id;
        }
      }
    }
    $cat_ids = implode(',', $cat_array);
    wp_list_categories('include='.$cat_ids.'&title_li=Author Categories');
    ?>

    thanks michael! thats definitely doing something however it looks like its just listing all the categories i have not specific ones the author has posts in. but i will keep working at it thank you!

    ampersandbox.net/?author=2

    open to solutions or alternatives im pretty stumped.

    Just create some links..
    http://ampersandbox.net/?author=2&cat=1 – working example link

    WordPress will sort out getting the posts that match for you..

    WOW i did not know you could do that thank you so much I will fool around with your idea and see if i cant come up with something thank you =)

    I grabbed this code and pieced some stuff together…but it looks like it does the same thing as Michael gave me

    http://wordpress.pastebin.ca/1660678

    It basically displays all categories, What I want to do is exclude the categories that an author has not posted in…

    There’s no need for any of that, what do you currently have in your author.php file? (aka the author template).

    Template files (index.php, archive.php etc..) already know what to do when query vars exist, the only time they can’t intercept and use those queried variables is when you over-ride the query, ie. you’re setting the query_posts line.

    If you can show me the code being used in the author template file, i can suggest changes or additions to allow the file to do what it should actually already be doing.

    A query like..

    query_posts('author=1&cat=1');

    should provide a list of posts in that category and by that author only, there should be nothing else required other then to ensure you are querying that page in a fashion that places the correct query vars into the query.

    Hello t31os_!

    Thank you for replying again. What you showed/told me before about adding ?author=1&cat=1 works great however I basically need to generate a list of categories on the author.php template

    each list should be different for each author because my goal is to have it only lists categories the author is posting in. currently the code i m using from michael and the other source lists all the categories even if the author has no posts. I basically need to exclude a category if an author has zero posts in.

    Example: kevin has written a post in category one and two and john has written a post only in category one

    therefore on kevins page it lists: one and two
    on johns page it lists: one

    here is the code michael provided for me:

    <?php
    //get all posts for an author, then collect all categories
    //for those posts, then display those categories
    $cat_array = array();
    $args=array(
     'author' => 4,
     'showposts'=>-1,
     'caller_get_posts'=>1
    );
    $author_posts = get_posts($args);
    if( $author_posts ) {
      foreach ($author_posts as $author_post ) {
        foreach(get_the_category($author_post->ID) as $category) {
          $cat_array[$category->term_id] =  $category->term_id;
        }
      }
    }
    $cat_ids = implode(',', $cat_array);
    wp_list_categories('include='.$cat_ids.'&title_li=Author Categories');
    ?>

    Ok i think i misunderstood before.

    However, you could use this method, since it’s a little faster..

    <?php
    $categories = $wpdb->get_results("
    	SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug
    	FROM $wpdb->posts as posts
    	LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
    	LEFT JOIN $wpdb->term_taxonomy as tax ON relationships.term_taxonomy_id = tax.term_taxonomy_id
    	LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
    	WHERE 1=1 AND (
    		posts.post_status = 'publish' AND
    		posts.post_author = '4' AND
    		tax.taxonomy = 'category' )
    	ORDER BY terms.name ASC
    ");
    ?>
    <ul>
    	<?php foreach($categories as $category) : ?>
    	<li>
    		<a href="<?php echo get_category_link( $category->ID ); ?>" title="<?php echo $category->name ?>"><?php echo $category->name ?></a>
    	</li>
    	<?php endforeach; ?>
    </ul>

    If it’s a small site and you’ve not got thousands of posts, you’ll likely not notice a difference though… but for the sake of following through … 😉

    If you want to find out the which author has been queried then it’s a simple case of..

    $author = get_query_var('author'); // Queried by ID

    or

    $author = get_query_var('author_name'); // Queried by name

    Add the variable into the other code, and you are essentially querying for categories for the current author being queried..

    This line..

    posts.post_author = '4'

    ..would become..

    posts.post_author = '$author'

    WOW i love you for this! thank you so much it works perfectly! exactly what i wanted. you are truly amazing =) thank you so much!!!!!

Viewing 15 replies - 1 through 15 (of 34 total)
  • The topic ‘[Resolved] How do I….list categories an author has wrote in on author.php’ is closed to new replies.