admin管理员组文章数量:1291309
I am trying to create and endpoint where blog posts can be searched by author first name and/or last name. I was trying to do this with WP_Query but it uses the user nice name for the search which is not working. Is there a way to do this using a built in query or do I need to do a custom SQL query?
add_action( 'rest_api_init', function () {
register_rest_route( 'blog-posts/v1', 'blog-by-author', array(
'methods' => 'POST',
'callback' => 'get_posts_by_author',
'permission_callback' => '__return_true',
) );
} );
function get_posts_by_author(WP_REST_Request $request) {
$data = $request->get_params();
$author_name = $data['author_name'];
// this uses nice name, not first/last name
$query = new WP_Query( array( 'author_name' => $author_name ) );
return print_r($query);
}
I am trying to create and endpoint where blog posts can be searched by author first name and/or last name. I was trying to do this with WP_Query but it uses the user nice name for the search which is not working. Is there a way to do this using a built in query or do I need to do a custom SQL query?
add_action( 'rest_api_init', function () {
register_rest_route( 'blog-posts/v1', 'blog-by-author', array(
'methods' => 'POST',
'callback' => 'get_posts_by_author',
'permission_callback' => '__return_true',
) );
} );
function get_posts_by_author(WP_REST_Request $request) {
$data = $request->get_params();
$author_name = $data['author_name'];
// this uses nice name, not first/last name
$query = new WP_Query( array( 'author_name' => $author_name ) );
return print_r($query);
}
Share
Improve this question
asked May 31, 2021 at 6:34
user8463989user8463989
5931 gold badge8 silver badges24 bronze badges
1 Answer
Reset to default 1Actually, when you set author_name
, WP_Query
will use get_user_by( 'slug', <author name> )
to find a user with the specified "nice name" (user_nicename
) value, and if found, then WP_Query
sets the author
arg to the found user's ID.
Therefore you can follow the same approach, but you would use get_users()
to find users having the specified first and/or last name and use the author
(or author__in
) arg with WP_Query
. E.g.
$users = get_users( array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $request['author_name'],
'compare' => 'LIKE',
),
array(
'key' => 'last_name',
'value' => $request['author_name'],
'compare' => 'LIKE',
),
),
'fields' => 'ID',
) );
if ( ! empty( $users ) ) {
$query = new WP_Query( array( 'author' => $users[0] ) );
// or to include all found users..
// $query = new WP_Query( array( 'author__in' => $users ) );
// ... your code.
}
Additional Notes
You don't have to do the
$data = $request->get_params();
because you could just access parameters via direct array access on theWP_REST_Request
object, e.g.$request['author_name']
like in my example above.You should register
author_name
usingargs
in the third parameter forregister_rest_route()
, or at least, check that it's not empty before callingget_users()
.I know the
print_r()
in your code is just for testing, but the function will actually echo the output unless if the second parameter is set totrue
..
本文标签: wp querysearch blog posts by author first name and or last name
版权声明:本文标题:wp query - search blog posts by author first name and or last name 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741529143a2383648.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论