Skip to content

Instantly share code, notes, and snippets.

@ann71727
Created October 27, 2017 19:04
Show Gist options
  • Save ann71727/8b89a8a0ed8229797c9dc8241aa48ffd to your computer and use it in GitHub Desktop.
Save ann71727/8b89a8a0ed8229797c9dc8241aa48ffd to your computer and use it in GitHub Desktop.
WP_Query get posts Array
<?php
/*
* Convert Object To Array
* https://v123.tw
*/
function WP_Query_arr(array $args){
$default = array(
'showposts' => -1,
'post_type' => 'post',
'post_status' => 'publish'
);
$args = array_merge($default,$args);
$result = new WP_Query($args);
$posts = array_map(
function( $post ) {
return (array) $post;
},
$result->posts
);
return $posts;
}
# EXAMPLE ======================
$args = array(
'post_type' => 'post',
);
$posts = WP_Query_arr($args);
print_r( $posts );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment