Last active
September 30, 2015 22:15
-
-
Save Page-Carbajal/8342e9bc6b76bd627d50 to your computer and use it in GitHub Desktop.
Get WordPress Posts By Meta
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 | |
function getPostByMeta( $metaKey, $metaValue, $operatos = '=' ){ | |
$args = array( | |
'showposts' => -1, | |
'meta_query' => array( | |
'relation' => 'AND', | |
array( | |
'key' => $metaKey, | |
'value' => $metaValue, | |
'compare' => $operator | |
) | |
) | |
); | |
$query = new \WP_Query( $args ); | |
// Verbose | |
$list = array(); | |
while( $query->have_posts() ){ | |
$query->the_post(); | |
$list[] = get_post() ; | |
} | |
// Concise | |
$list = $query->get_posts() // Returns an Array of WP_Post objects or an Empty array | |
wp_reset_postdata(); | |
return $list; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment