Cleverness – WordPress Resources

Get Categories Author has Posted in for Custom Post Types

I needed to list all the categories of a custom post type that an author had posted in.

I found code to do that with regular posts in the wordpress.org forums and I modified it to use with custom post types.

Be sure to replace custom_taxonomy with the name of your taxonomy.

Place this into your theme file where you want the list to appear.

<?php
$author = get_the_author_meta('ID');

$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 = '$author' AND
	tax.taxonomy = 'custom_taxonomy' )
ORDER BY terms.name ASC
");
?>
<ul>
<?php foreach( $categories as $category ) : ?>
	<li>
		<a href="<?php echo get_term_link( $category->name, 'custom_taxonomy' ); ?>" title="<?php echo $category->name ?>"><?php echo $category->name ?></a>
	</li>
<?php endforeach; ?>
</ul>

3 Comments

  1. JR

    Hi,

    I tried to use your example. What it says it does – is exactly what i’m looking for.

    But i have tried using terms and post types, with no avail.

    • Cindy

      As someone pointed out below, customposttype should have been the name of your custom taxonomy instead of the custom post type. Other than that I’m not sure. It’s been three years since I posted this code and there are probably better ways to go about it.

  2. Mr.JCRP

    The code worked for me but only after tweaking a little bit.
    1. Its not ‘customposttype’. It should be ‘custom_taxonomy’. With ‘customposttype’ it never worked.
    2. Please explain the code as we would all like to learn.
    3. Also please share the wordpress.org link from where you found the original code. It would be helpful for us to learn more about it.

Trackbacks/Pingbacks

  1. Managing Multiple Authors: Customizing the WordPress Author Page « Lorelle on WordPress

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Use [php]code goes here[/php] to post PHP code ([js],[css], and [sql] also available)

*

Notify me of followup comments via e-mail. You can also subscribe without commenting.