Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress - get post based on meta field content

Tags:

php

wordpress

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

like image 356
dremme Avatar asked Jun 17 '12 03:06

dremme


People also ask

How do I get post meta value in WordPress?

get_post_meta( int $post_id, string $key = '', bool $single = false ) Retrieves a post meta field for the given post ID.

How do I use Post Meta in WordPress?

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.

What is meta value WordPress?

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.


1 Answers

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); 
like image 136
colllin Avatar answered Sep 19 '22 22:09

colllin