1

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);
| improve this question | | | | |
2

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.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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