WordPress – Show Posts by User/Author/Displayname
A good colleague of mine is using his domain with WordPress for his entire family, but want to have a page per family member showing only their posts.
Although this might not be the exactly right solution, it could be done by:
- Installing the plugin Exec-PHP for WordPress which allows to excute PHP code in widgets, posts and pages and can work on multi userlevels with restrictions.
- Find the display name of the user who’s posts you want to display.
- Create a page in WordPress and insert the code:
<?php query_posts(‘display_name=INSERT_DISPLAY_NAME_HERE’); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;?>
- Now go to the page…. My test is here PeterDB’s Page
I will try to update the code so it shows the posts in the same style as the front page, so more to come.
UPDATE: I modified the PHP code, so it shows the same style of layout as the “normal WordPress posts”
<?php query_posts(‘display_name=INSERT_DISPLAY_NAME_HERE’); ?>
<?php while (have_posts()) : the_post(); ?>
<h2><a class=”title” href=”<?php the_permalink() ?>” rel=”bookmark”><?php the_title(); ?></a></h2>
<div class=”info”>
<span class=”date”><?php the_time(‘F jS, Y’) ?></span>
<div class=”act”>
<span class=”comments”><?php comments_popup_link(__(‘No comments’), __(’1 comment’), __(‘% comments’)); ?></span>
<?php edit_post_link(__(‘Edit’), ‘<span class=”editpost”>’, ‘</span>’); ?>
</div>
</div>
<div class=”fixed”></div>
<div class=”content”>
<?php the_content(); ?>
<p class=”under”>
<span class=”author”><?php the_author_posts_link(); ?></span>
<span class=”categories”><?php the_category(‘, ‘); ?></span>
<span class=”tags”><?php the_tags(”, ‘, ‘, ”); ?></span>
</p>
<div class=”fixed”></div>
</div>
<?php endwhile;?>
UPDATE: If you copy past the above code you need to change the ยด with ‘ for some unknown reason WordPress replaces them. You can download a text file with the code here: PHP Code Posts from User
UPDATE: After testing with a different theme I noticed that the code was not that theme independant. So I change the code and added some descriptive test around the date, comments, author, catagory and tags. Although it does not look like the original format used by your theme, it comes close enough. Of course you can always edit it to make it look more accurate. Code in TXT file posts_from_user_updated.
<?php query_posts(‘display_name=INSERT_DISPLAY_NAME_HERE’); ?>
<?php while (have_posts()) : the_post(); ?>
<h2><a class=”title” href=”<?php the_permalink() ?>” rel=”bookmark”><?php the_title(); ?></a></h2>
<div class=”info”>
<span class=”date”>Posted: <?php the_time(‘F jS, Y’) ?></span>
<div class=”act”>
<span class=”comments”>Comments: <?php comments_popup_link(__(‘No comments’), __(’1 comment’), __(‘% comments’)); ?></span>
<?php edit_post_link(__(‘Edit’), ‘<span class=”editpost”>’, ‘</span>’); ?>
</div>
</div>
<div class=”fixed”></div>
<div class=”content”>
<?php the_content(); ?>
<p class=”under”>
<span class=”author”>Author: <?php the_author_posts_link(); ?></span>
<span class=”categories”>Category: <?php the_category(‘, ‘); ?></span>
<span class=”tags”>Tags: <?php the_tags(”, ‘, ‘, ”); ?></span>
</p>
<div class=”fixed”></div>
</div>
<?php endwhile;?>
UPDATE: I did an actual test with multiple users on my own site and it turns out that query_posts does not work with display_name only really with author_name, so I needed to update the code again. TXT here posts_from_user_updated_again. You can see two test pages here: PeterDB’s Page and specialK’s Page.
<?php query_posts(‘author_name=INSERT_USER_LOGIN_NAME_HERE’); ?>
<?php while (have_posts()) : the_post(); ?>
<h2><a class=”title” href=”<?php the_permalink() ?>” rel=”bookmark”><?php the_title(); ?></a></h2>
<div class=”info”>
<span class=”date”>Posted: <?php the_time(‘F jS, Y’) ?></span>
<div class=”act”>
<span class=”comments”>Comments: <?php comments_popup_link(__(‘No comments’), __(’1 comment’), __(‘% comments’)); ?></span>
<?php edit_post_link(__(‘Edit’), ‘<span class=”editpost”>’, ‘</span>’); ?>
</div>
</div>
<div class=”fixed”></div>
<div class=”content”>
<?php the_content(); ?>
<p class=”under”>
<span class=”author”>Author: <?php the_author_posts_link(); ?></span>
<span class=”categories”>Category: <?php the_category(‘, ‘); ?></span>
<span class=”tags”>Tags: <?php the_tags(”, ‘, ‘, ”); ?></span>
</p>
<div class=”fixed”></div>
</div>
<?php endwhile;?>