I'm retrieving a list of posts from various sites in a multisite installation using the following method:

$posts = lc_main_eh_posts();

foreach ( $posts as $post ) {

    if ( $post->blog_id == get_current_blog_id() ) {
        get_template_part('list-medium', get_post_format());
    } else {
        get_template_part('list-medium_auxblog', get_post_format());

Where lc_main_eh_posts() retrieves the posts from all sites using WP_Query and switch_to_blog() and returns an ordered array of WP_Posts. It also assigns the blog id each post belongs to in $post->blog_id for later use.

In list-medium_auxblog.php, I'm doing:


    $content = strip_tags(strip_shortcodes(get_the_content()));
    $trimmed_content = wp_trim_words($content, 20, '...');

    echo $trimmed_content;


The problem arises when there's a shortcode in use in a post that belongs to a different blog/site. I've tried apply_filters('the_content', get_the_content), do_shortcode(get_the_content()), and strip_shortcodes() is in use there and it doesn't even work. The shortcodes just appear as raw text.

I also tried adding switch_to_blog() and restore_current_blog() before and after the template tags shown above, with no luck.

I suspect this is due to the fact that those shortcodes are not registered network-wide. I can't just enable them, because they display data that is only in the database of specific sites.

What other options do I have? How can I parse shortcodes from posts that belong to other sites in the MS as if the current blog was that one ?

  • Are you trying to strip specific shortcodes from the_content, or do you want to remove everything that's in [square_brackets]? – Pat J Jun 13 '14 at 23:28
  • No, I want to display*/*render shortcodes from other sites – Alain Jacomet Forte Jun 15 '14 at 14:51
  • Per the switch_to_blog() documentation, plugins are not switched in a switch_to_blog() call. So you'd need to have the shortcode's plugins active on your origin site. – Pat J Jun 15 '14 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.