Wordpress - get post based on meta field content

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 05:55:05

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( "post_type=player&meta_key=player_team&meta_value=$teamname&order=ASC" );
colllin

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!