Lajos Arpad Lajos Arpad - 5 years ago 200
PHP Question

How to filter posts by author on edit.php

Let's suppose the user visits the page of

http://mywpsite/wp-admin/edit.php?author=john-doe


Let's suppose there is an author with the name of
'John Doe'
and the given author has three posts. Yet, when I visit the page, I see an empty grid, as if there were no posts created by this author.

I would like to search for posts created by the given user. Based on my research, I can see that people are claiming that something like this should work:

function posts_for_current_author($query) {

if($query->is_admin) {

global $user_ID;
$query->set('author', $user_ID);
}
return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');


Source.

However, here the author's ID is expected as input, yet, I do not know how to get the author's id by the slug of
'john-doe'
.

How can I get the ID of the author by slug and search for posts based on that ID?

EDIT:

This is one failed try, based on NATH's comment:

function wpshock_search_filter( $query ) {
if ((is_admin()) && (isset($_GET["author"])) && (preg_match('/[^a-zA-Z_0-9]/i', $_GET["author"]))) {
$query->set("author_name", $_GET["author"]);
}
return $query;
}
add_filter('pre_get_posts','wpshock_search_filter');


The query still returns no elements. I have
var_dump
ed
$query
and seen that
$query->query["author"]
has the textual problem, which is a potential problem. Also,
$query->tax_query
contains data related to author. I am sorry if this question is worthy of down-votes, I thought others might be confused by Wordpress's database handling as well and thus this question might be useful. Maybe I was wrong.

Answer Source

WordPress provides an author_name query variable to you. You can use the following by default:

http://mywpsite/wp-admin/edit.php?author_name=john-doe

Read more about public query variables in the Codex.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download