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.