I have a custom php page and i need to override the global posts variables, but i cant get it to work. When going through the main loop im able to override the global posts, but i cant override whatever have_posts() is using to obtain the count. Im obtaining data from a 3rd party API so i need to build the WP object on the fly and then override the default objects returned from the default query. I have the following code and its working, but the problem is that i get a result that contains the post and then several null objects afterwards.
require_once('../../../wp-blog-header.php');
global $post, $posts, $found_posts, $post_count;
$post->ID = 99999999999;
$post->post_content = "TEST PAGE content";
$post->post_title = "Page Title";
$post->post_name = "test";
$posts = array($post);
$post_count = 1;
$found_posts = 1;
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
var_dump($post);
}
}
The above code generates the following output. I need to get rid of the NULLs. To do this i need to make it so have_posts() only returns true once.
object(WP_Post)#2975 (24) { ["ID"]=> int(99999999999) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2016-01-21 19:50:24" ["post_date_gmt"]=> string(19) "2016-01-21 19:50:24" ["post_content"]=> string(17) "TEST PAGE content" ["post_title"]=> string(10) "Page Title" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(4) "test" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2016-01-21 19:50:24" ["post_modified_gmt"]=> string(19) "2016-01-21 19:50:24" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(36) "" ["menu_order"]=> int(0) ["post_type"]=> string(4) "page" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } NULL NULL NULL NULL NULL NULL NULL NULL NULL