• Heyas, I’m trying to get lastest WP blog posts into my site’s front page using wp-blog-header.php.

    The problem is that I get this error if I have the code inside a function:
    Fatal error: Call to undefined method stdClass::set_prefix() in /wordpress/wp-settings.php on line 268

    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’s in the “root” of the php file. The main problem of course is that the site I’m working with is made so that everything is basically being run inside a function call.

    The full PHP code:

    loadItAll();
    
    function loadItAll() {
    	require_once('wp-blog-header.php');
    	$myposts = get_posts('numberposts=5&offset=1&category=1');
    	foreach($myposts as $post) :
    	?>
    		<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
    	<?php endforeach;
    }

Viewing 7 replies - 1 through 7 (of 7 total)
  • Currently having this same problem. Ever find a solution?

    I’m having the same problem and ended up querying the database directly to extract posts. A fix would be appreciated – it seems very brittle integration, given the number of results when searching the web for this error message!

    Yeah, very weird that nobody seems to care too much.

    This is a really frustrating problem to not have an answer to.

    I was having the same problem you guys are having. After pulling my hair off for a few hours, I got to solve my problem, here it goes…

    I was sending some information via AJAX to the functions.php file, under my TEMPLATEPATH directory. On the top of the functions.php file, I was including a function that checked if there was something set in the $_POST variable, then executed a specific action.

    And guess what?, the error was the same you guys are having, so I started to get deep into the problem.

    For some reason, if you use any php file used by wordpress internally, as the POST action for your AJAX request or form, it doesn’t load the WordPress environment correctly, so I decided to create a separate php file, unrelated to the theme itself, called it postdata.php and added the following code:

    // Available actions
    $actions = Array('request_gallery_slot');
    
    if (count($_POST)) {
    		if ( in_array($_POST['action'], $actions) ) {
    			// Initialize WordPress
    			$wp_root = explode('wp-content', $_SERVER['SCRIPT_FILENAME']);
    			$wp_root = $wp_root[0];
    			chdir($wp_root);
    			if (!function_exists('add_action')) {
    				if (file_exists('wp-load.php')) {
    					require_once('wp-load.php');
    				} else {
    					require_once('wp-config.php');
    				}
    			}
    
    			// Determine the action
    			switch ( $_POST['action'] ) {
    				case 'request_gallery_slot':
    					$slot = $_POST['slot'];
    					get_gallery_slot($slot);
    			}
    		}
    	}

    That is working perfectly, hope this helps.

    I am having the same trouble. I can call the functions from external file and that works. The problem I have is I run three blogs and want to get latest posts from each blog and display them on a landing page.

    Check out this post.

    This problem occured for me when I included wp-blog-header.php from within a function (class method actually). This caused the global variables that WP sets to not be global for real, thus not being accessible by calling global $var. You get the error because $wpdb is global, but not really. I solved this by adding:

    global $wpdb

    Beneath the line: (in wp-settings.php on the line 269)

    require_wp_db();

    So the result looks like this:

    require (ABSPATH . WPINC . '/compat.php');
    require (ABSPATH . WPINC . '/functions.php');
    require (ABSPATH . WPINC . '/classes.php');
    
    require_wp_db();
    global $wpdb; // Look look! Over here! line 270 in wp-settings.php
    
    if ( !empty($wpdb->error) )
    	dead_db();

    This however did not solve the problem. The same kind of error popped up elsewhere because of dependencies of other global variables in other places.

    I just added the include(‘blog/wp-blog-header.php’); in my index.php outside of any functions (this is not the best solution for me because it means I have to include WP in all my page requests even when WP is not needed.)

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Calling wp-blog-header.php from inside a PHP function’ is closed to new replies.