Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Theme Development Custom Post Type Templates in WordPress Coding Your Own Custom Post Type Templates

I need to set the WP_Query in this example so that it only displays content based on the author.

I want to modify this query so that it only shows the "songs" for the given author. So, if the author is "Bob" then I only want to output his songs. Of course the author can change so the author needs to be a variable and not hard coded ( $author ?).

$args = array( 'post_type' => 'song' ); $query = new WP_Query( $args );

I've tried this and it works if I put the correct authors nice name in the right place

$args = array( 'post_type' => 'song', 'author_name' => 'Jimmy' ); $query = new WP_Query( $args );

But I need the author name to fill dynamically based on the author of the page being looked at. How do I do this?

Thank you!

2 Answers

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

Hi, Trevor. According to the WP documentation, it is possible to do that using the author parameters in your query (see the first section under "Parameters" in the link). You can get the author name by author (int), author_name (string), author__in (array), and even by author__not_in (array).

Hi Ryan,

Thanks for your help! I may have edited my question after your answer and didn't see it. I'm not sure.

Anyway, in the second example above you'll see I'm setting the 'author_name' => 'Jimmy' - but I want 'Jimmy' to be dynamically populated by the author of the page.

So I tried this: 'author_name' => $author_name

But it doesn't work. Any ideas?

Ryan Field
Ryan Field
Courses Plus Student 21,242 Points

I'm not a WP expert just quite yet, but I think it depends on how the users are getting to that particular page. Do you have your custom post type 'song' set up in a way that each individual post includes the author as a category or tag? If that's the case, WP should automatically be able to filter all songs by that author and display them as an archives page (like the default action that happens when you click on a blog category page).

If you don't have that kind of thing set up, you could use $_GET variables where the links to each author looks like this:

<a href="page.php?id=102">Author 102</a>

and then your PHP for the page where they would be displayed would look like:

<?php

$author_id = $_GET['id'];

$args = array(
     'post_type' => 'song',
     'author' => $author_id
);
$query = new WP_Query( $args );

?>

Of course, if you do that, you'll need to perform proper sanitization and ensure that invalid author IDs redirect or something, but I think letting WP doing its thing automatically is probably the best idea.

Thanks Ryan. I'll try this in just a bit and let you know. But I understand what you're saying and maybe you're right that this is the best way.

Ryan Field
Ryan Field
Courses Plus Student 21,242 Points

Alrighty! Give it a go and let us know if you run into any other issues! :)

Thanks Ryan! This worked. I just need to figure out what to do when there is no id passed, etc. (error handling as you said). But that's not as pressing an issue.

Thanks for your help.