Skip to content

Instantly share code, notes, and snippets.

@robertdevore
Created August 12, 2022 02:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertdevore/8c8bb131d1ea8ef4a9691f87a737453e to your computer and use it in GitHub Desktop.
Save robertdevore/8c8bb131d1ea8ef4a9691f87a737453e to your computer and use it in GitHub Desktop.
<?php
/**
* Get array of post ID's with specific meta key and value
*
* @param string $meta_key - The meta key you are checking for
* @param string $meta_value - The meta value for the meta key you're checking for
* @param string $post_type - The post type to use with get_posts() - default: post
*
* @return array
*/
function acme_post_ids_array( $meta_key, $meta_value, $post_type = 'post' ) {
// Create array.
$post_ids = array();
// Args for posts.
$args = array(
'meta_query' => array(
array(
'key' => $meta_key,
'value' => $meta_value,
'compare' => '='
)
),
'numberposts' => -1,
'offset' => 0,
'post_type' => $post_type,
'post_status' => 'publish',
);
// Filter the args.
$args = apply_filters( 'acme_post_ids_array_args', $args );
// Get all posts.
$posts = get_posts( $args );
// Loop through posts.
foreach( $posts as $the_post ) {
// Add post ID to array.
$post_ids[] = $the_post->ID;
}
// Filter the array.
return apply_filters( 'acme_post_ids_array', $post_ids );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment