1

I have 5 Network sites in my WordPress multisite setup.

One of which is the "primary website", which the rest of the other network sites will be retrieving data from.

I am currently using WP_Query(); to retrieve posts and switch_to_blog() / restore_current_blog(); before and after the call to WP_Query(); if the current site is not the primary site.

Posts fetched from the mainsite are retrieved just fine.

When I go to any other site in the network which is NOT the primary site, it's supposed to switch to the primary site, retrieve posts from a custom post type AND a custom taxonomy. Once posts are found, restore the current blog.

However, the sub sites are returning ZERO posts. I thought it was strange. So I dumped/compared the results from WP_Query(); on the primary site and any of the sub sites.

Specifically noting the request portion of the queried object.

Request when on primary website:

[request] => SELECT SQL_CALC_FOUND_ROWS  ples_posts.ID FROM ples_posts  INNER JOIN ples_term_relationships ON (ples_posts.ID = ples_term_relationships.object_id) WHERE 1=1  AND ( ples_term_relationships.term_taxonomy_id IN (16) ) AND ples_posts.post_type = 'mbe-sales-listings' AND ((ples_posts.post_status = 'publish')) GROUP BY ples_posts.ID ORDER BY ples_posts.post_date DESC LIMIT 0, 5

Request when on any sub site:

[request] => SELECT SQL_CALC_FOUND_ROWS  ples_posts.ID FROM ples_posts  WHERE 1=1  AND 0 = 1 AND ples_posts.post_type = 'mbe-sales-listings' AND ((ples_posts.post_status = 'publish')) GROUP BY ples_posts.ID ORDER BY ples_posts.post_date DESC LIMIT 0, 4

The posts per page are intended to be different, so that doesn't matter.

The point is, when the query is requested from any of the sub sites, it appears to be querying for the custom post type, but it's not even considering the custom taxonomy.

Note:

The custom post type, and all custom taxonomies for that custom post type are only registered on the primary site.

When registering the custom post type, and all of the custom taxonomies for that custom post type on all of the sub sites as well, it then retrieves the posts as intended.

I don't particularly want to register the custom post type or any of the custom taxonomies on any of the sub sites. I only want them registered on the primary site.

Now that you know the elaborate story, here comes the question...

Is there any way I can only have the custom post type and all custom taxonomies registered on the primary site, and still be able to retrieve the necessary posts from any of the sub sites?

I'm assuming it is possible with a special $wpdb; query and NOT possible using switch_to_blog();, WP_Query();, and restore_current_blog();.

If it's possible using a special $wpdb; query, I would appreciate guidance on a safe and proper SQL query in order to do so. I'm just looking to query all published posts from a specified site ID, post type, and taxonomy / term.

1 Answer 1

1

After some thought, I came up with a possible "solution". However, it wasn't exactly the route I hoped to take.

The custom post type and all custom taxonomies for that custom post type must be registered on ALL network sites in order for WP_Query(); to fetch posts from the primary site from any of the sub sites in the network.

At least as a temporary solution, I have specified the following settings to help "hide" the custom post types and custom taxonomies from the sub sites.

Firstly, I specified two global variables to use through out my plugin. One for the primary site ID, and another for the current site ID. These variables can then be globalized into any class methods or functions.

$GLOBALS['mbe_primary_site_id'] = 1;
$GLOBALS['mbe_current_site_id'] = get_current_blog_id();

While registering the custom post type... I added a check, and altered some of the post type registration arguments.

global $mbe_primary_site_id, $mbe_current_site_id;
if($mbe_current_site_id != $mbe_primary_site_id){
    $args['show_ui'] = false;
    $args['public'] = false;
    $args['show_in_menu'] = false;
}

While registering the custom taxonomies... I added a check, and altered some of the taxonomy registration arguments.

global $mbe_primary_site_id, $mbe_current_site_id;
if($mbe_current_site_id != $mbe_primary_site_id){
    $args['show_ui'] = false;
    $args['public'] = false;
}

So now, I can query posts by taxonomy from the primary site on any of the sub sites like: switch_to_blog($mbe_primary_site_id);, WP_Query(...);, and restore_current_blog();

I don't know if there's a real solid way around this. What I do know is this method works, and if you were to do it via $wpdb; queries, there would be quite some conditional logic and checks involved, especially if you plan on paginating the content.

It would just be more of a pain and a lot of extra work to use $wpdb; for everything, when you could just register the custom post types and custom taxonomies on the sub sites as well, and just hide access to them on the sub sites.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.