Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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
You can’t perform that action at this time.