admin管理员组文章数量:1122832
So, here is the issue:
I am using a plugin named User Submitted Posts, and it works great on its part. However, I still want to have that user content page the Twenty Twenty Four theme has by default, which you can access when you click on the user's name. The issue is, USP doesn't require logins, so these posts are all actually submitted by the same user, whose display name is being overridden to show as the user_submit_name, a custom field in USP's posts.
This makes the author archive display all the posts, because technically they are all assigned the same author.
What I want is to modify the query on the archive page to sort through posts using this custom field, so that it still works. I am a complete beginner though, so this is proving rather difficult.
The main issue I'm facing is that I know how to modify the author archive query, but I don't know how to pass the user_submit_name it should use to the function. Basically, I have no way of telling the archive for which name it should be sorting, aside from hard coding it.
Here is my code so far:
function wpd_author_query( $query ) {
if ( $query->is_author()
&& $query->is_main_query() ) {
// your code to set $current_user_name here
$query->set( 'meta_key', 'user_submit_name' . /*author name*/);
$query->set( 'posts_per_page', 4);
// EDIT
// unset the requested author
unset( $query->query_vars['author_name'] );
}
}
add_action( 'pre_get_posts', 'wpd_author_query' );
To recap, I need to somehow pass the custom field "user_submit_name" from a post to the function that modifies the author query, when the viewer clicks on the author link.
Does anyone know how I could go about this? Also if you have any better ideas how to do this, which you probably do, feel free to share them. Thank you.
So, here is the issue:
I am using a plugin named User Submitted Posts, and it works great on its part. However, I still want to have that user content page the Twenty Twenty Four theme has by default, which you can access when you click on the user's name. The issue is, USP doesn't require logins, so these posts are all actually submitted by the same user, whose display name is being overridden to show as the user_submit_name, a custom field in USP's posts.
This makes the author archive display all the posts, because technically they are all assigned the same author.
What I want is to modify the query on the archive page to sort through posts using this custom field, so that it still works. I am a complete beginner though, so this is proving rather difficult.
The main issue I'm facing is that I know how to modify the author archive query, but I don't know how to pass the user_submit_name it should use to the function. Basically, I have no way of telling the archive for which name it should be sorting, aside from hard coding it.
Here is my code so far:
function wpd_author_query( $query ) {
if ( $query->is_author()
&& $query->is_main_query() ) {
// your code to set $current_user_name here
$query->set( 'meta_key', 'user_submit_name' . /*author name*/);
$query->set( 'posts_per_page', 4);
// EDIT
// unset the requested author
unset( $query->query_vars['author_name'] );
}
}
add_action( 'pre_get_posts', 'wpd_author_query' );
To recap, I need to somehow pass the custom field "user_submit_name" from a post to the function that modifies the author query, when the viewer clicks on the author link.
Does anyone know how I could go about this? Also if you have any better ideas how to do this, which you probably do, feel free to share them. Thank you.
Share Improve this question asked Jun 9, 2024 at 14:59 GrondGrond 11 Answer
Reset to default 0To create a custom field archive in WordPress where posts are filtered by a user_submit_name
custom field, follow these steps:
Register a Query Variable: This enables WordPress to recognize and use this variable in the URL.
function register_query_vars( $vars ) { $vars[] = 'user_submit_name'; return $vars; } add_filter( 'query_vars', 'register_query_vars' );
Modify the Archive Query: Adjust the existing
pre_get_posts
function to use the newly registered query variable.function wpd_author_query( $query ) { if ( $query->is_author() && $query->is_main_query() ) { $user_submit_name = get_query_var('user_submit_name'); if (!empty($user_submit_name)) { $query->set( 'meta_key', 'user_submit_name' ); $query->set( 'meta_value', $user_submit_name ); $query->set( 'posts_per_page', 4 ); unset( $query->query_vars['author_name'] ); } } } add_action( 'pre_get_posts', 'wpd_author_query' );
This setup enables filtering posts by the custom field user_submit_name
without requiring hardcoded values.
本文标签: pluginsWhat is the easiest way to create a custom field archive
版权声明:本文标题:plugins - What is the easiest way to create a custom field archive? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736304288a1932183.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论