php Wordpress - 根据元字段内容获取帖子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11068795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wordpress - get post based on meta field content
提问by dremme
I'm developing a wordpress plugin. I'm using two different custom post types, playersand teams.
我正在开发一个 wordpress 插件。我使用了两种不同的自定义帖子类型,玩家和团队。
- Playershas relevant meta fields: First name, last name, and team.
- Teamshas relevant meta fields of team name.
- 玩家有相关的元字段:名字、姓氏和团队。
- 团队具有团队名称的相关元字段。
While editing a specific teampost, I'm trying to have an array of all the playersthat currently have that team'sname posted to their meta field for team name. I'm not sure how to do this. Any help or articles would be really helpful. Thanks
在编辑特定的团队帖子时,我试图将当前拥有该团队名称的所有球员的数组发布到他们的团队名称元字段中。我不知道该怎么做。任何帮助或文章都会非常有帮助。谢谢
采纳答案by jn_pdx
The important thing is that you are querying for posts using at least the three criteria of the post type, meta key, and meta value.
重要的是,您至少使用帖子类型、元键和元值这三个条件来查询帖子。
For example, let's assume your custom post type is just called "player" And, each 'player' post has a meta field attached called "player_team"
例如,假设您的自定义帖子类型仅称为“玩家”并且每个“玩家”帖子都附加了一个名为“player_team”的元字段
You could then query for those posts using something like this:
然后,您可以使用以下内容查询这些帖子:
$teamname = ""; // the player's team that you're querying for
$myquery = new WP_Query( "post_type=player&meta_key=player_team&meta_value=$teamname&order=ASC" );
回答by colllin
Or using get_posts:
或使用get_posts:
$args = array(
'meta_key' => 'player_team',
'meta_value' => $teamname,
'post_type' => 'player',
'post_status' => 'any',
'posts_per_page' => -1
);
$posts = get_posts($args);
Another equivalent query using meta_queryinstead of meta_keyand meta_value:
另一个使用meta_query代替meta_key和的等效查询meta_value:
$args = array(
'meta_query' => array(
array(
'key' => 'player_team',
'value' => $teamname
)
),
'post_type' => 'player',
'posts_per_page' => -1
);
$posts = get_posts($args);

