Created
October 27, 2017 19:04
-
-
Save ann71727/8b89a8a0ed8229797c9dc8241aa48ffd to your computer and use it in GitHub Desktop.
WP_Query get posts Array
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 | |
/* | |
* 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