Opened 3 years ago

Last modified 8 months ago

#14511 new enhancement

new function - wp_get_sites($args)

Reported by: transom Owned by:
Priority: normal Milestone: Future Release
Component: Multisite Version:
Severity: normal Keywords: has-patch needs-testing
Cc: garyc40@…, mikeschinkel@…, mailings@…, dennen@…, pbaylies, pippin@…

Description (last modified by nacin)

With the deprication of get_blog_list (and the dire warnings), we had a need for a function to retrieve a list of blogs. At first blush, it appears that one could use get_blogs_of_user. It seems to assume that the same admin (userid 1) is going to create all sites. If you have multiple network_admins, then a search for a single user's sites won't return a full list.

So I have adapted the code from wp-admin/ms-sites.php and with a nod to wp_list_pages - am submitting wp_get_sites for a future release.

This function accepts an argument list to filter the results, modify the order, and limit the range of results. I wrote it with an eye to provide an interface consistent with other wp functions, as well as potentially replacing the sql query in wp-admin/ms-sites.php.

The initial set of arguments include:

			'include_id' 		,				// includes only these sites in the results, comma-delimited
			'exclude_id' 		,				// excludes these sites from the results, comma-delimted
			'blogname_like' 	,				// domain or path is like this value
			'ip_like'			,				// Match IP address
			'reg_date_since'	,				// sites registered since (accepts pretty much any valid date like tomorrow, today, 5/12/2009, etc.)
			'reg_date_before'	,				// sites registered before
			'include_user_id'	,				// only sites owned by these users, comma-delimited
			'exclude_user_id'	,				// don't include sites owned by these users, comma-delimited
			'include_spam'		=> false,		// Include sites marked as "spam"
			'include_deleted'	=> false,		// Include deleted sites
			'include_archived'	=> false,		// Include archived sites
			'include_mature'	=> false,		// Included blogs marked as mature
			'public_only'		=> true,		// Include only blogs marked as public
			'sort_column'		=> 'registered',// or registered, last_updated, blogname, site_id
			'order'				=> 'asc',		// or desc
			'limit_results'		,				// return this many results
			'start'				,				// return results starting with this item

The usual warning are provided. It works for me but I haven't tested it completely yet. But I am assuming this could be helpful to others and I have no doubt about getting comments back <grin>

Attachments (4)

wp-get-sites.php (4.5 KB) - added by transom 3 years ago.
Source for wp_get_sites
wp_get_sites.diff (4.7 KB) - added by transom 3 years ago.
Add wp_get_sites to ms-functions.php
wp-get-sites.2.php (4.4 KB) - added by transom 3 years ago.
Standalone version of wp_get_sites() - toss in functions.php to test
wp-get-sites.3.php (3.7 KB) - added by pbaylies 8 months ago.
Standalone version of wp_get_sites() - toss in functions.php to test

Download all attachments as: .zip

Change History (23)

transom3 years ago

Source for wp_get_sites

  • Keywords has-patch added; multisite get_blogs_by_user removed

Pretty neat.

Found a bug will submit new patch shortly (joining on the registration log causes interesting problems vis-a-vie blogs not created via registration - duh!)

Attached is a patch for wp-includes/ms-functions.php (replacing the farked version)

  • Description modified (diff)

This probably generates quite a bit of notices (turn on WP_DEBUG) since most of the variables being extracted don't actually exist in the array. include_id is being considered a value, not the key -- should be include_id => '' instead.

Lots of LIKEs and such. This query probably isn't very efficient, but it's a start we can work off of.

Changing the "isset" to not-empty (after defining defaults for all strings).

I am confused by your comment re: variables being extracted not existing in the array - after the wp_parse_args there should be a value (even if it is the default) for each variable.

Include_id is supposed to be a string of blog_ids to be included in the results.

Will post new diff shortly.

transom3 years ago

Add wp_get_sites to ms-functions.php

transom3 years ago

Standalone version of wp_get_sites() - toss in functions.php to test

Updated diff and attached file for wp_get_sites that can used in functions.php to test functionality

comment:8 follow-up: ↓ 9   kahless3 years ago

Copied wp_get_sites2.php into my themes functions.php and am getting

Undefined property: wpdb::$site_id in /path-to-wp-content/wp-content/themes/delicious/functions.php on line 159

and line 159 is

$query .= "WHERE b.site_id = '{$wpdb->site_id}' ";

I am running the latest nightly build.

comment:9 in reply to: ↑ 8 ; follow-up: ↓ 11   kahless3 years ago

Replying to kahless:

Copied wp_get_sites2.php into my themes functions.php and am getting

Undefined property: wpdb::$site_id in /path-to-wp-content/wp-content/themes/delicious/functions.php on line 159

and line 159 is

$query .= "WHERE b.site_id = '{$wpdb->site_id}' ";

I am running the latest nightly build.

I am running a subdirectory install if that makes any difference.

  • Keywords 3.2-early added
  • Milestone changed from Awaiting Review to Future Release

Will likely need some thought and a new patch.

comment:11 in reply to: ↑ 9   cesperanc2 years ago

Replying to kahless:

Replying to kahless:

Copied wp_get_sites2.php into my themes functions.php and am getting

Undefined property: wpdb::$site_id in /path-to-wp-content/wp-content/themes/delicious/functions.php on line 159

and line 159 is

$query .= "WHERE b.site_id = '{$wpdb->site_id}' ";

I am running the latest nightly build.

I am running a subdirectory install if that makes any difference.

I've the same problem here. Renaming {$wpdb->site_id} to {$wpdb->siteid} seams to resolve the problem.

  • Cc garyc40@… added
  • Keywords needs-patch added; has-patch removed
  • Cc mikeschinkel@… added

No caching?

  • Keywords 3.2-early removed

Not for 3.2

  • Cc mailings@… added
  • Cc dennen@… added

Standalone version of wp_get_sites() - toss in functions.php to test

  • Cc pbaylies added
  • Keywords has-patch needs-testing added; needs-patch removed

I've uploaded a new patch for this, rewritten the original without the join and some of the options, and most of the LIKEs; all data in the query should be known or sanitized as well. So what do we need to do to get a function like this back into core.

  • Cc pippin@… added
Note: See TracTickets for help on using tickets.