32

I'm developing a wordpress plugin. I'm using two different custom post types, players and teams.

  • Players has relevant meta fields: First name, last name, and team.
  • Teams has relevant meta fields of team name.

While editing a specific team post, I'm trying to have an array of all the players that currently have that team's name 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

2 Answers 2

68

Or using 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_query instead of meta_key and meta_value:

$args = array(
    'meta_query' => array(
        array(
            'key' => 'player_team',
            'value' => $teamname
        )
    ),
    'post_type' => 'player',
    'posts_per_page' => -1
);
$posts = get_posts($args);
4
  • 7
    If you set 'posts_per_page' => -1 that will retrieve all matching posts, no need for 999999.
    – cfx
    Dec 19, 2013 at 23:27
  • 9
    or set nopaging => true. Mar 17, 2014 at 18:29
  • 1
    @vortexs Thanks for the 'post_status' suggestion!
    – colllin
    Mar 10, 2015 at 23:46
  • @cfx If you know there will be few posts you shouldn't worry. E.g.: Team members in a small business. It never will return hundreds of results :P Feb 1, 2018 at 15:21
25

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"

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( array(
    'post_type' => 'player',
    'meta_key' => 'player_team',
    'meta_value' => $teamname,
    'order' => 'ASC'
));
5
  • 2
    Since this is a secondary query inside the team page, it's recommended to use WP_Query or get_posts instead of query_posts - see the Codex
    – Hobo
    Jun 17, 2012 at 6:35
  • 3
    Updated answer to reflect this
    – jnpdx
    Jun 17, 2012 at 8:10
  • 1
    +1. Nice one, thanks. By the way (sorry, I didn't notice this before) - it's probably worth adding a posts_per_page parameter of -1, to ensure you get all players back (or noting it, anyway, so the asker's aware of the possibility).
    – Hobo
    Jun 17, 2012 at 13:32
  • Thanks!! This helps a lot. I was trying to do this with "get_posts" but couldn't seem to get the parameters right.
    – dremme
    Jun 17, 2012 at 16:52
  • Also my CPT was just called "player" not "players". Your solution works perfectly.
    – dremme
    Jun 17, 2012 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.