Created
June 8, 2017 17:16
-
-
Save johnpbloch/3129c031e7b6b6035bc361ac05ef5d25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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