How to get all posts ids that has a meta key and value.
$meta_key = 'hide_rss';
$meta_value="yes";
$post_ids = array(1, 2, 3);
How to get all posts ids that has a meta key and value.
$meta_key = 'hide_rss';
$meta_value="yes";
$post_ids = array(1, 2, 3);
Use the meta_key
, meta_value
, and fields
parameters.
Example using get_posts()
: (Take note though, get_posts()
ignores or doesn’t include sticky posts.)
$meta_key = 'hide_rss';
$meta_value="yes";
$post_ids = get_posts( [
'meta_key' => $meta_key,
'meta_value' => $meta_value,
'fields' => 'ids',
] );
Or using new WP_Query()
:
$meta_key = 'hide_rss';
$meta_value="yes";
$q = new WP_Query();
$post_ids = $q->query( [
'meta_key' => $meta_key,
'meta_value' => $meta_value,
'fields' => 'ids',
] );
See https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters for other possibilities, or advanced meta query.