I created a shortcode which filters and lists some posts. Some of them are standard posts, some of them contains content mirror. When I add the shortcode to the TinyMCE Editor, all works fine.
Now I wanted to do the filtering and listing automatically without pasting my shortcode into 1000 pages (via custom fields). So i created the following function. It adds the shortcode to the_content without pasting it in the editor, it is only mentioned in PHP.
function post_filter($content)
{
global $post;
global $content_is_mirror;
// Metadata/ Custom Fields
$version = get_post_meta($post->ID, 'version', true);
$product = get_post_meta($post->ID, 'product', true);
$status = get_post_meta($post->ID, 'post_status', true);
// Posts with Content Mirror Shortcode
if (isset ( $content_is_mirror )) { return; }
$content .= '<h1>Filter</h1>';
$content .= do_shortcode('[posts version="' . $version . '" product="' . $product . '" post_status="' . $status . '"]');
return $content;
}
add_filter('the_content','post_filter');
If I add this line, the page is broken (only on pages that contain posts with content mirror):
$content .= do_shortcode('[posts version="' . $version . '" product="' . $product . '" post_status="' . $status . '"]');
So i added a temporary solution (found on the FAQs of this plugin):
global $content_is_mirror;
if (isset ( $content_is_mirror )) { return; }
Now, the site is fine, it shows all what it should.
BUT my table of contents does not appear anymore, maybe because I did a return, but nothing else seems to fix it.
If I echo
the shortcode, all is fine (but without table of contents), but I really need to add it to post content, because it is important for generating a table of contents.
Now I suppose the reason is
– $content .= ...
– return $content;
– add_filter('the_content', 'post_filter');