Skip to content

Instantly share code, notes, and snippets.

@johnpbloch
Created June 8, 2017 17:16
Show Gist options
  • Save johnpbloch/3129c031e7b6b6035bc361ac05ef5d25 to your computer and use it in GitHub Desktop.
Save johnpbloch/3129c031e7b6b6035bc361ac05ef5d25 to your computer and use it in GitHub Desktop.
<?php
// Let's get a list of Post IDs that have given post meta item matching a pattern, but we only want unique values
// And we want the items ordered correctly so that keys aren't skipping numbers, etc.
$post_ids = array_values( array_unique( wp_list_pluck( array_filter( (new WP_Query(['posts_per_page' => -1]))->posts, function($post) {
return (bool) preg_match( '/some-pattern/i', $post->some_meta_value );
} ), 'ID' ) );
<?php
// Now compare this to using collections
$post_ids = collect( (new WP_Query(['posts_per_page' => -1]))->posts )
->filter( function($post) { return (bool) preg_match( '/some-pattern/i', $post->some_meta_value ); } )
->pluck( 'ID' )
->unique()
->values()
->all();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment