Hi Anton,
Initially I was using the loop but because of many issues with social sharing and event management plugins that add content to anywhere they see the loop I changed this to get_post.
Another advantage of using get_post is that this should be faster.
Could you provide me with a specific scenario for which you need the loop? I could see if there is anything I can do about your issue.
The actual point where it is going wrong is when using a social media plugin.
The social media plugin uses the the_content
filter to add the social media buttons. In the code for adding the social media buttons there is code comparable like this:
if ( is_page() ) {
// Display social media buttons
}
The problems occurs when having a sidebar on that page that has the custom post widget in it. the_content
filter is called when displaying the custom post but because the global $post
variable is still set to a page object (the page we are on) the plugin thinks it should display the social media buttons at the end of the_content filter.
I don’t think the social media plugin is doing something unreasonable but this might still be a very tricky issue to fix.
For other people dealing with the same issue, I build a workaround to fix this issue. I added a shortcode to all custom blocks named [hide_social_media_buttons]
then I filtered the_content
twice, once on priority 9 and once on priority 11. In the first filter I remove the social media button filter if the shortcode is present and in the second filter I re-add the social media button filter if I removed it.