I'm creating a custom template for my website and I'm currently using this code in order to loop to my multisite worpress and get the latest posts:
global $post;
global $wp_query;
$subsites = get_sites();
foreach( $subsites as $subsite ) {
$subsite_id = get_object_vars($subsite)["blog_id"];
$subsite_name = get_blog_details($subsite_id)->blogname;
switch_to_blog($subsite_id);
$blog_posts = get_posts();
restore_current_blog();
foreach( $blog_posts as $post ) {
setup_postdata( $post );
get_template_part( 'theme-partials/post-templates/loop-content/masonry' );
}
}
Inside the template_part the theme is trying to get the category for the post but return an empty array.
if ( pixelgrade_option( 'blog_show_categories' ) ) {
$categories = get_the_category();
if ( ! is_wp_error( $categories ) && ! empty( $categories ) ) { ?>
How could I get the category information inside the loop? What's wrong in my code?
I'm creating a custom template for my website and I'm currently using this code in order to loop to my multisite worpress and get the latest posts:
global $post;
global $wp_query;
$subsites = get_sites();
foreach( $subsites as $subsite ) {
$subsite_id = get_object_vars($subsite)["blog_id"];
$subsite_name = get_blog_details($subsite_id)->blogname;
switch_to_blog($subsite_id);
$blog_posts = get_posts();
restore_current_blog();
foreach( $blog_posts as $post ) {
setup_postdata( $post );
get_template_part( 'theme-partials/post-templates/loop-content/masonry' );
}
}
Inside the template_part the theme is trying to get the category for the post but return an empty array.
if ( pixelgrade_option( 'blog_show_categories' ) ) {
$categories = get_the_category();
if ( ! is_wp_error( $categories ) && ! empty( $categories ) ) { ?>
How could I get the category information inside the loop? What's wrong in my code?
Share Improve this question asked Mar 27, 2020 at 16:59 CrazYoshiCrazYoshi 33 bronze badges 3 |1 Answer
Reset to default 0The problem is here:
switch_to_blog($subsite_id);
$blog_posts = get_posts();
restore_current_blog();
foreach( $blog_posts as $post ) {
setup_postdata( $post );
get_template_part( 'theme-partials/post-templates/loop-content/masonry' );
}
Your code fetches a number of posts, but then you switch back to the original blog! So now post number 5 means something completely different than it did before.
You need to stay in that other blogs context for as long as you're working with that blogs data, or all the references will be incorrect. Once you restore
the original blog you can't use that data.
Additionally, switching to another blog just changes the data that comes back, it won't load in plugins and code. This means only the filters, taxonomies, post types of the original blog are loaded.
get_posts
without specifying'suppress_filters' => false
WP can't use any caching or object caches, slowing things down considerably. It would be easier to just use a normalWP_Query
loop instead. You should also check thatswitch_to_blog
actually worked, but this code doesn't check if it returned true or false – Tom J Nowell ♦ Commented Mar 27, 2020 at 17:26get_posts
function internally callWP_Query
. In addition the functionswitch_to_blog
always return true, is it correct? How can I change the get_posts into a WP_Query? Could you give me an example? – CrazYoshi Commented Mar 28, 2020 at 8:36WP_Query
should make it obvious from the official docs. But comments aren't for answering questions or asking new ones – Tom J Nowell ♦ Commented Mar 28, 2020 at 16:41