I'm developing a wordpress plugin. I'm using two different custom post types, players and teams.
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
get_post_meta( int $post_id, string $key = '', bool $single = false ) Retrieves a post meta field for the given post ID.
You can select the meta key from the drop down list in case you want to add the same post meta even to this post, or you can define a new one by clicking on entering new. Once you click on enter now you can add the post meta and the value and then click on Add Custom field as shown below.
The value is the information that will appear in the meta-data list on each individual post that the information is associated with. In simpler terms, WordPress allows us to write custom information to the database, associate it with any post we'd like, and then retrieve it as needed.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With