Skip to content

Instantly share code, notes, and snippets.

@rbk
Created June 8, 2016 19:55
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 rbk/225b97e3870d1adc4dbb37018d16cfbf to your computer and use it in GitHub Desktop.
Save rbk/225b97e3870d1adc4dbb37018d16cfbf to your computer and use it in GitHub Desktop.
Query WordPress and add the metadata to the result.
<?php
/*
*
* Get posts with a standard query
* Include all the meta data in the result
*
*/
function get_posts_with_meta( $args ) {
$query = get_posts($args);
for($i=0;$i<count($query);$i++) {
$meta = get_post_meta( $query[$i]->ID );
foreach( $meta as $key => $value ) {
$query[$i]->{$key} = $value[0];
}
}
return $query;
}
$args = array(
'post_type' => 'page',
'posts_per_page' => -1
);
$query = get_posts_with_meta( $args );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment