{"id":1128097,"date":"2009-07-02T10:53:21","date_gmt":"2009-07-02T10:53:21","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/calling-wp-blog-headerphp-from-inside-a-php-function\/"},"modified":"2016-08-19T13:16:24","modified_gmt":"2016-08-19T13:16:24","slug":"calling-wp-blog-headerphp-from-inside-a-php-function","status":"closed","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/calling-wp-blog-headerphp-from-inside-a-php-function\/","title":{"rendered":"Calling wp-blog-header.php from inside a PHP function"},"content":{"rendered":"<p>Heyas, I&#8217;m trying to get lastest WP blog posts into my site&#8217;s front page using wp-blog-header.php.<\/p>\n<p>The problem is that I get this error if I have the code inside a function:<br \/>\n<code>Fatal error: Call to undefined method stdClass::set_prefix() in \/wordpress\/wp-settings.php on line 268<\/code><\/p>\n<p>If I put the same code straight into some empty file, it works, returning the news. If I put the same code inside a function, it fails. So, basically it only works if it&#8217;s in the &#8220;root&#8221; of the php file. The main problem of course is that the site I&#8217;m working with is made so that everything is basically being run inside a function call.<\/p>\n<p>The full PHP code:<\/p>\n<pre><code>loadItAll();\n\nfunction loadItAll() {\n\trequire_once(&#039;wp-blog-header.php&#039;);\n\t$myposts = get_posts(&#039;numberposts=5&amp;offset=1&amp;category=1&#039;);\n\tforeach($myposts as $post) :\n\t?&gt;\n\t\t&lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot;&gt;&lt;?php the_title(); ?&gt;&lt;\/a&gt;&lt;br \/&gt;\n\t&lt;?php endforeach;\n}<\/code><\/pre>\n","protected":false},"template":"","class_list":["post-1128097","topic","type-topic","status-closed","hentry","topic-tag-external","topic-tag-function","topic-tag-php","topic-tag-set_prefix","topic-tag-variable-scope"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/1128097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/1128097\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=1128097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}