Discussion:
Using WP_Query "outside" of Wordpress by including wp-load.php inside a function
Chris Richard
2013-12-12 04:13:12 UTC
Permalink
I get a couple referencing errors when trying to include wp-load.php from
within a PHP function on WP Multi Site.

public static function getEntries($options) {
@include_once('wordpress/wp-load.php');
if (function_exists('switch_to_blog')) {
switch_to_blog('...');

$options = array_merge(
array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 10,
),
$options);

$query = new WP_Query($options);
...

After poking around a bit I found that I can make two small edits to make
this whole scenario work nicely.

in ms-settings.php:

global $wpdb;

and in wp-settings.php:

$GLOBALS['wp_query'] = $wp_the_query; // instead of $wp_query = ...

SO everything works great until we upgrade and all the changes get
clobbered. These changes are pretty minor - is this something that could be
committed? Does WP do pull requests?

Chris
Dion Hulse (dd32)
2013-12-12 04:20:39 UTC
Permalink
For your particular use-case, you should be able to add
global $wpdb, $wp_query;
to before the include_once() line.

However, you may want to re-test under 3.8, I believe we altered a few
things in 3.7&3.8 to work better when included within a function..
Post by Chris Richard
I get a couple referencing errors when trying to include wp-load.php from
within a PHP function on WP Multi Site.
public static function getEntries($options) {
@include_once('wordpress/wp-load.php');
if (function_exists('switch_to_blog')) {
switch_to_blog('...');
$options = array_merge(
array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 10,
),
$options);
$query = new WP_Query($options);
...
After poking around a bit I found that I can make two small edits to make
this whole scenario work nicely.
global $wpdb;
$GLOBALS['wp_query'] = $wp_the_query; // instead of $wp_query = ...
SO everything works great until we upgrade and all the changes get
clobbered. These changes are pretty minor - is this something that could be
committed? Does WP do pull requests?
Chris
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Chris Richard
2013-12-12 05:28:58 UTC
Permalink
TY
Post by Dion Hulse (dd32)
For your particular use-case, you should be able to add
global $wpdb, $wp_query;
to before the include_once() line.
However, you may want to re-test under 3.8, I believe we altered a few
things in 3.7&3.8 to work better when included within a function..
Post by Chris Richard
I get a couple referencing errors when trying to include wp-load.php from
within a PHP function on WP Multi Site.
public static function getEntries($options) {
@include_once('wordpress/wp-load.php');
if (function_exists('switch_to_blog')) {
switch_to_blog('...');
$options = array_merge(
array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 10,
),
$options);
$query = new WP_Query($options);
...
After poking around a bit I found that I can make two small edits to make
this whole scenario work nicely.
global $wpdb;
$GLOBALS['wp_query'] = $wp_the_query; // instead of $wp_query = ...
SO everything works great until we upgrade and all the changes get
clobbered. These changes are pretty minor - is this something that could
be
Post by Chris Richard
committed? Does WP do pull requests?
Chris
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Loading...