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?
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 falseget_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?WP_Query
should make it obvious from the official docs. But comments aren't for answering questions or asking new ones